Home>

Create four pull-down menus, and the list contents of the second pull-down menu change depending on the acquired value of the first pull-down menu. Do this with all four pull-down menus.
In the example, "upper body, lower body" is stored in the first pull-down menu, and when the upper body is selected, "head, chest, belly" can be selected in the second pull-down menu. If i select the head from the second pull-down menu, you will be able to select "mouth, eyes" from the third pull-down menu. I want to pass the value of the pull-down menu to the later pull-down menu continuously.

I can get the list of up to the second pull-down menu, but I can't get the list from the third pull-down menu
Corresponding source code
#Get list by selecting pull-down menu (1st)
def cb2_selected (event):
    var1 = cb1.get ()
    if var1 =='upper body':
        val1 = ('head','chest','belly')
    elif var1 =='lower body':
        val1 = ('leg')
    else: else:
        val1 = ('')
    cb2 = ttk.Combobox (main_win, state ='readonly')
    cb2 ['value'] = val1
    cb2.grid (row = 1, column = 1)
#Get list by selecting pull-down menu (second)
def cb3_selected (event):
    var2 = cb2.get ()
    if var2 =='head':
        val2 = ('mouth','eye')
    elif var2 =='chest':
        val2 = ('chest','arms')
    elif var2 =='belly':
        val2 = ('navel')
    elif var2 =='legs':
        val2 = ('peach','ashi')
    else: else:
        val2 = ('')
    cb3 = ttk.Combobox (main_win, state ='readonly')
    cb3 ['value'] = val2
    cb3.grid (row = 1, column = 2)
#Get list by selecting pull-down menu (third)
def cb4_selected (event):
    var3 = cb3.get ()
    if var3 =='mouth':
        val3 == ('teeth')
    elif var3 =='eyes':
        val3 = ('pupil')
    elif =='chest':
        val3 = ('')
    elif var3 =='arm':
        val3 = ('hand','finger')
    elif var3 =='navel':
        val3 = ('')
    elif var3 =='peach':
        val3 = ('')
    elif var3 =='feet':
        val3 = ('finger')
    else: else:
        val3 = ('')
    cb4 = ttk.Combobox (main_win, state ='readonly')
    cb4 ['value'] = val3
    cb4.grid (row = 1, column = 3)
#Pull-down menu binding settings
cb1.bind ('<<ComboboxSelected >>', cb2_selected)
cb1.bind ('<<Return >>', cb2_selected)
cb2.bind ('<<ComboboxSelected >>', cb3_selected)
cb2.bind ('<<Return >>', cb3_selected)
cb3.bind ('<<ComboboxSelected >>', cb4_selected)
cb3.bind ('<<Return >>', cb4_selected)
What I tried

In the previous post, there was a question that the list contents of the second pull-down menu change depending on the acquired value of the first pull-down menu, and I created this program with reference to it, but it worked well. I did not go.
It seems that the value of cb2 could not be obtained by get (), but the cause is unknown.

Supplementary information (FW/tool version, etc.)

Execution environment
Windows 10
Python 3.8.5

  • Answer # 1

    It seems that the value of cb2 could not be obtained by get (), but the cause is unknown.

    This is because the cb2 Combobox is created in a local variable inside each function.
    The global variable cb2 in another function points to another widget.

    I don't know where it is because I think it is out of the range of the code posted, but
    Since it is cb2.bind, if you are making a widget somewhere globally,
    This can be solved by creating the widget only the first time.

    In each function, for widgets that have already been generated
    Change only the value of the item.


    cb2 = ttk.Combobox (...)
    def cb2_selected (event):
      cb2 = ttk.Combobox (...) #<--- No need here
      # This cb2 becomes a local variable
    def cb3_selected (event):
      cb2.get () # refers to the global variable combobox
    cb2.bind (...)