Skip to content
Advertisement

Get Value of Combobox, while they are bound

I created two Comboboxes in Tkinter. In the first Box you select the Material, the second box reacts to the selected Value of the first box and changes the available options. Its maybe not the best way coded, but it works.

My Problem is, I want to know which colour is selected.

Because if I print (Box_2.get()) it would be empty.

win = Tk()
win.title("Window")

Material_List = ["Material1","Material2"]
Colour_List = ["Green","Blue","Black","White"]

def Print_Colour():     #should print the selected colour 
    print(Box_2.get())

def Select_Mat(eventObject):  #bound to the Material selection and will change the color options. 
    Material_Choice = Box_1.get()
    if Material_Choice == Material_List[0]:
        Box_2 = Combobox (win, values=[Colour_List[0],Colour_List[1]], width=17, state='readonly')
        Box_2.grid(row=1, column=4, columnspan=2)
    elif Material_Choice == Material_List[1]:
        Box_2 = Combobox (win, values=[Colour_List[2],Colour_List[3]], width=17, state='readonly')
        Box_2.grid(row=1, column=4, columnspan=2)

Box_1 = Combobox (win, values=[Material_List[0],Material_List[1]], width=17, state='readonly')
Box_1.grid(row=1, column=1, columnspan=2)

Box_2 = Combobox (win, width=17, state='readonly')
Box_2.grid(row=1, column=4, columnspan=2)

Box_1.bind("<<ComboboxSelected>>", Select_Mat)

Print_Button = Button (win, text="Print Colour", width=16, command=Print_Colour) 
Print_Button.grid(row=2, column=1, columnspan=2)


win.mainloop()

Advertisement

Answer

Try this:

from tkinter import *
from tkinter.ttk import Combobox


win = Tk()
win.title("Window")

Material_List = ["Material1","Material2"]
Colour_List = ["Green","Blue","Black","White"]

def Print_Colour():     #should print the selected colour 
    print(Box_2.get())

def Select_Mat(eventObject):  #bound to the Material selection and will change the color options. 
    Material_Choice = Box_1.get()
    if Material_Choice == Material_List[0]:
        Box_2.config(values=[Colour_List[0], Colour_List[1]])
    elif Material_Choice == Material_List[1]:
        Box_2.config(values=[Colour_List[2], Colour_List[3]])
    Box_2.set("") # Clear Box_2's selection

Box_1 = Combobox(win, values=[Material_List[0], Material_List[1]], width=17, state='readonly')
Box_1.grid(row=1, column=1, columnspan=2)

Box_2 = Combobox(win, width=17, state='readonly')
Box_2.grid(row=1, column=4, columnspan=2)

Box_1.bind("<<ComboboxSelected>>", Select_Mat)

Print_Button = Button(win, text="Print Colour", width=16, command=Print_Colour) 
Print_Button.grid(row=2, column=1, columnspan=2)


win.mainloop()

The problem with your code is that you keep creating new Box_2 widgets. Instead you should just use the .config method to change their values. Also each time the user selects something different from Box_1, it will clear Box_2‘s selection.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement