Two Tkinter questions (long)

Grant Edwards ge at nowhere.none
Wed Jul 5 18:49:20 EDT 2000


In article <sn66qkgrfv.fsf at motorola.com>, Aaron Ginn wrote:

>The problem is that when I create the file browser in a
>toplevel widget, the command that my buttons are supposed to execute
>are executed immediately.  For instance, here's a portion of code:
>
>class FileBrowser:
>
>    def __init__(self, obj, dir):
>
>< SNIP a lot of wigdets for brevity >
>
>	self.Filter = Button(self.button_frame, text = 'Filter', \
>		      command = self.do_filter())

    self.Filter = Button(self.button_frame, text = 'Filter', \
                      command = self.do_filter)

>self.filter_entry.bind('<Return>', lambda s=self: s.do_filter())

Functions bound to events need to take an event as a parameter
(if you don't want the event, you can use lambda to discard it):

self.filter_entry.bin('<Return>', labda event: self.do_filter())

or you can define another function that will accept an event
parameter:

def do_filter(self, *args)

Now you can just do

self.filter_entry.bin('<Return>', self.do_filter)


>Is there a problem with binding an event to a lambda function?  

No.  The problem is that normal button command functions are
passed 0 arguments and event functions are passed one argument
(the event).

-- 
Grant Edwards                   grante             Yow!  I am a traffic light,
                                  at               and Alan Ginzberg kidnapped
                               visi.com            my laundry in 1927!



More information about the Python-list mailing list