我正在使用组合框编写程序,而我定义了组合框,显示的结果是None
,但如果我没有定义组合框,结果就是我想要的。
由于我的程序需要修改组合框,如何使用显示None
的结果解决问题?
import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title("search store")
ttk.Label(win, text="select a area:").grid(column=0, row=0)
def search():
action.configure( text = "SEARCH" )
print(e())
def e():
cmb = tk.StringVar()
cmb1 = ttk.Combobox(win, width=30, textvariable=cmb,state='readonly')
cmb1['values'] = ('place 1', 'place 2', 'place 3', 'place 4' )
cmb1.grid(column=0, row=2)
cmb1.current(0)
action = ttk.Button(win, text="SEARCH", command=search)
action.grid(column=2, row=1)
win.mainloop()
答案 0 :(得分:0)
搜索按钮命令回调需要传递对组合框的引用。
通过在搜索按钮命令回调中设置组合框来访问此引用的当前解决方法不是解决此问题的正确方法。
设置组合框。然后在设置Button时,将对combobox的引用传递给Button命令callback 例如
from tkinter import Tk, StringVar
from tkinter.ttk import Label, Combobox, Button
win = Tk()
def search(cmb1):
print(cmb1.get())
def setup_combo_box():
cmb1 = Combobox(win, width=30, textvariable=StringVar(), state='readonly')
cmb1['values'] = ('place 1', 'place 2', 'place 3', 'place 4' )
cmb1.grid(column=0, row=2)
cmb1.current(0)
return cmb1
cmb1 = setup_combo_box()
action = Button(win, text="SEARCH", command=lambda: search(cmb1))
action.grid(column=2, row=1)
win.mainloop()