0
import tkinter

window = tkinter.Tk()

def abc(event):
     ans=0
    numberss=['7','8','9']
    omenu2['menu'].delete(0, 'end')
    for number in numberss:
        omenu2['menu'].add_command(label=numberss[ans], command=efg)
        ans=ans+1


def efg(event=None):
    print('yee')

numbers = ['1','2', '3']
number=['4','5','6']

var = tkinter.StringVar(window)
var1 = tkinter.StringVar(window)


omenu = tkinter.OptionMenu(window, var, *numbers, command = abc)
omenu.grid(row=1)
omenu2 = tkinter.OptionMenu(window, var1, *number, command = efg)
omenu2.grid(row=2)

after you have entered the first option menu, it will update the second one. when you enter data into the second one, it runs the command, but doesn't show you what you entered. i do not want to include a button, and i know that the command works and not on the second

i found some code that changed the options of the second menu, however when i ran this, the command wouldn't work as it was changed to tkinter.setit (i would also like to know what is does. i do not currently understand it)

omenu2['menu'].add_command(label=numberss[ans], command=tkinter._setit(var1, number))

this has been taken from a larger piece of code, and has thrown the same error

  • There are couple dupes out there: [Tkinter GUI: Update choices of an option menu depending on a choice from another option menu](https://stackoverflow.com/questions/19794069/tkinter-gui-update-choices-of-an-option-menu-depending-on-a-choice-from-another) or [Changing the options of a OptionMenu when clicking a Button](https://stackoverflow.com/questions/17580218/changing-the-options-of-a-optionmenu-when-clicking-a-button) **tl;dr:** set your variable(var1 for this). – Lafexlos Jul 21 '17 at 13:01

1 Answers1

1

You should set your StringVar(var1) new value.

def abc(event): 
    numberss=['7','8','9']
    omenu2['menu'].delete(0, 'end')
    for number in numberss:
        omenu2['menu'].add_command(label=number, command=lambda val=number: efg(val))

def efg(val, event=None):
    print('yee')
    var1.set(val)

You are using for loop so you don't need ans(at least not in this code) since it iterates over items themselves.

Lafexlos
  • 7,618
  • 5
  • 38
  • 53