How to query object of GUI?

Scott David Daniels Scott.Daniels at Acm.Org
Wed Apr 15 19:43:09 CEST 2009


John McMonagle wrote:
> Muddy Coder wrote:
>> I need to query the ID of GUI, in Tkinter, but don't know how to do
>> it. This is my code: ...
> 
> Pass the name of the label as an argument to the callback.  Below is a
> short working example:
>     ...
>     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))
 >     ...

Another way to do the same thing (a way to "avoid" lambda):
     from functools import partial
     ...
     class MyGUI(object):
         ...
         def make_menu(self):
             top = Menu(self)
             menObj = Menu(top)
             for n, lab in enumerate(read_from_database()):
                 menObj.add_command(label=lab,
                             command=partial(self.do_menu, n, lab))

         def do_menu(self, sequence, label):
             # here you know it was the (sequence+1)th entry named label
             ...

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list