我试图通过从下拉列表中获取用户输入来运行查询。我有多个下拉菜单,并且想根据用户选择更改后续的下拉菜单。例如,如果有两个下拉菜单,则州和城市。如果有人选择一个城市,则该州下拉菜单中应该只有一个州,而有人选择一个州,则只有该州的城市才能显示。我试图访问用户单击下拉菜单时选择的变量,以便我可以运行特定查询并操纵查询。但是,只有当用户单击确认按钮并调用具有的函数时,我才能执行此操作var.get()
。
conn = pyodbc.connect("{Connection String}")
cursor = conn.cursor()
cursor.execute('query to get states;')
states = cursor.fetcall()
state_list = []
for state in states:
state_list.append(state[0])
def on_change_selection(value):
state = selected_state.get()
cursor.execute('select cities from XYZ where state = \'%s\'' %(state))
cities = cursor.fetcall()
city_list = []
for city in cities:
city_list.append(city[0])
'''At this point I want the city_list to get updated in the already existing drop down option of cities'''
root = Tk()
selected_state = StringVar(value=state_list[0])
op_state = OptionMenu(root, selected_state, *(state_list), command=on_change_selection)
op_state.pack()
select_city = StringVar (value = city_list[0])
op_city = OptionMenu(root, selected_city, *(city_list), command=some_function())
op_city.pack()
我在想的解决方案只是在每个函数中的窗口都使用新选项进行更新的过程中感到困惑。我相信有更好的方法可以做到这一点。
另外,如果我想使用ComboBox并基本上使用很多过滤器,您是否建议使用PyQt5?我以前从未使用过GUI。先感谢您。
OptionMenu
具有command=
与相似的选项Button
。每当您在其中更改选择时,它均用于执行功能OptionMenu
import tkinter as tk
def on_change_selection(value):
print(' value:', value)
print('selected:', selected.get())
root = tk.Tk()
options = ["one", "two", "three"]
selected = tk.StringVar(value="one")
#selected = tk.StringVar()
#selected.set("one")
op = tk.OptionMenu(root, selected, *(options), command=on_change_selection)
op.pack()
root.mainloop()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句