I am trying to run queries by getting user inputs from drop downs. I have multiple drop downs and would like to change subsequent drop downs based on user selections. For example, if there are two drop downs state and city. If someone chooses a city, then there should be only one state in the state drop down and is someone selects a state, then only cities in that state should show up. I was trying to access the variable the user selects when he/she clicks on the drop down so that I can run specific queries and manipulate the queries. But I can only do it when the user clicks the confirm button and calls the function that has 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()
The solution that I was thinking is just convoluted where within every function the Window gets updated with new option. I am sure there is a better way of doing this.
Also, if I want to use ComboBox and basically a lot of filters, do you suggest using PyQt5? I have never used GUIs before. Thank you in advance.