原谅绿色。尝试构建GUI,其中从Combobox中选择的选项填充文本框。什么都没发生。第一次编程,所以欣赏我在这里犯了很多错误。
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
# function to display course selected
def courseDisplay():
box = course.get()
print(box)
# Create instance
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","Ascot", "Bath", "Chester")
courseChosen.grid(column=5, row=1,rowspan = 3, columnspan = 3,padx = 300, pady = 40)
courseChosen.current(0)
courseChosen.bind("<<ComboboxSelected>>", courseDisplay)
# create scrolled Text control
scrolW = 46
scrolH = 10
box = scrolledtext.ScrolledText(win, width=scrolW, height=scrolH, wrap=tk.WORD)
box.grid(column=5, row=8, columnspan=3,padx = 300,pady = 10)
# Start GUI
win.mainloop()
答案 0 :(得分:2)
由于在组合框上发生某些事件时调用函数courseDisplay
(即,当选择某个选项时),它应该接受一个变量(通常称为event
)。所以,你的函数应该是这样的:
def courseDisplay(event=None):
box = course.get()
print(box)
当然,您应该添加另一个逻辑来在文本框中显示测试而不是print
。