user1517644
user1517644

Reputation:

Python tkinter menu + variable

I've got a menu with multiple items on it, each item on the menu uses the same GUI for the functionality. Each desired function or menu item just has different things going on in the background. I know could make multiple copies of my gui code, assign it to an individual def, and then give the menu item the specific command line.

However is there an easier way which lets me specify my gui code once in a given def, and for Tkinter/python to run that def which recognizes which option was selected?

So for example:

filemenu.add_command(label="Item-a", command=gui)
filemenu.add_command(label="Item-b", command=gui)
filemenu.add_command(label="Item-c", command=gui)

which could launch def gui, which before drawing the interface can tell Item a, b or c were selected?

Hopefully this makes sense.

Any ideas pls?

Thanks

Upvotes: 1

Views: 2042

Answers (1)

mgilson
mgilson

Reputation: 309841

If I understand your question correctly, you could do something like this:

def gui(item):
    if item == 'a':
       pass # do something with a
    elif item == 'b':
       pass # do something with b

Now in your filemenu:

filemenu.add_command(label='Item-a', command=lambda : gui('a'))
filemenu.add_command(label='Item-b', command=lambda : gui('b'))

and so on.

Upvotes: 3

Related Questions