How to query object of GUI?

John McMonagle jmcmonagle at NO.SPAM.velseis.com.au
Wed Apr 15 01:36:38 EDT 2009


Muddy Coder wrote:
> 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



More information about the Python-list mailing list