How to query object of GUI?

M

Muddy Coder

Hi Folks,

I need to query the ID of GUI, in Tkinter, but don't know how to do
it. This is my code:

calss MyGUI:
........
def make_menu(self):
top = Menu(self)
menObj = Menu(top)
labels = read_from_database()
for lab in labels:
menObj.add_command(label=lab, command=self.do_menu)
def do_menu(self):
# here I need query which menu item was clicked

For GUI Entry, there is a resource name textvariable, so I can use
textvariable to distinguish one Entry from the others. But, I can't
find such a resource name for Menu. I also noticed label is an
identifier, but I have no idea how to get it. Can somebody help me
out? Thanks in advance!


Muddy Coder
 
J

John McMonagle

Muddy said:
Hi Folks,

I need to query the ID of GUI, in Tkinter, but don't know how to do
it. This is my code:

calss MyGUI:
........
def make_menu(self):
top = Menu(self)
menObj = Menu(top)
labels = read_from_database()
for lab in labels:
menObj.add_command(label=lab, command=self.do_menu)
def do_menu(self):
# here I need query which menu item was clicked

Pass the name of the label as an argument to the callback. Below is a
short working example:


from Tkinter import *

root = Tk()

def do_menu(l):
print 'menu pressed is ', l

menubar = Menu(root)
lab = 'test'
menubar.add_command(label=lab, command=lambda l=lab: do_menu(l))
lab = 'quit'
menubar.add_command(label=lab, command=lambda l=lab: do_menu(l))
root.config(menu=menubar)

root.mainloop()



Regards,

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top