无法在文本框中使用组合框选择填充文本框。没有错误返回

时间:2016-11-20 16:29:27

标签: python tkinter

因此创建了小部件,但文本框中没有填充组合框中的选择。感谢有人能指出我正确的方向

import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
#from courses import NationalHunt


win = tk.Tk() 
win.resizable(130,130)   
win.title("RaceCourse GUI")

# create combobox
course = tk.StringVar()
courseChosen = ttk.Combobox(win, width=60,textvariable=course,state='readonly')
courseChosen['values'] = ("Choose a course","Aintree","Ascot","Ayr", "Bangor",
        "Carlisle","Cartmel","Catterick","Cheltenham","Chepstow","Doncaster",
        "Exeter","Fakenham","Fontwell","Haydock","Hereford","Hexham","Huntingdon",
        "Kelso","Kempton","Leicester","Ludlow","Market Rasen","Musselburgh",
        "Newbury","Newcastle","Newton Abbot","Perth","Plumpton","Sandown",
      "Sedgefield","Southwell","Stratford","Taunton","Towcester","Uttoxeter",
        "Warwick","Wetherby","Wincanton","Windsor","Worcester")
courseChosen.grid(column=5, row=1,rowspan = 3, columnspan = 3,padx = 300, pady = 40)
courseChosen.current(0)
courseChosen.bind("<<ComboboxSelected>>",course.get())

scrolW  = 46
scrolH  =  10
box = scrolledtext.ScrolledText(win, width=scrolW, height=scrolH,bd=5,wrap=tk.WORD)
box.grid(column=5, row=8, columnspan=3,padx = 300,pady = 10)
box.delete("1.0","5.20")
box.insert(tk.INSERT,course.get())

# Start GUI
win.mainloop()

1 个答案:

答案 0 :(得分:0)

您的绑定不正确。它必须是&#34;

courseChosen.bind("<<ComboboxSelected>>",course.get)

绑定引用可调用的引用。您正在立即调用get方法,然后将结果传递给绑定。