0

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.

1 Answer 1

2

OptionMenu has option command= similar to Button. It is used to execute function everytime you change selection in 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()
Sign up to request clarification or add additional context in comments.

2 Comments

Another issue that I have been facing is that when the list becomes huge, OptionMenu does not help. I am trying to use ComboBox with a scroll bar. Does that have a similar command = feature? Thank you. This is exactly what I was looking for.
you can use bind() to assign function to Combobox - simple example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.