Tkinter button not working as expected

vagrantbrad at yahoo.com vagrantbrad at yahoo.com
Fri Sep 22 15:32:39 EDT 2006


Thank you Fredrik.  It makes total sense now that you've explained it
this way.  I frustrated at my ignorance on the assignment issue :)




Fredrik Lundh wrote:
> vagrantbrad at yahoo.com wrote:
>
> > I've created a short test program that uses tkFileDialog.askdirectory
> > to help the user input a path into a tk entry widget.  The problem I'm
> > having is that when I run the code as listed below, the getPath
> > function is called when the program initially runs, not when the button
> > is pressed.
>
> the right side of an assignment is always evaluated before it is assigned, so
>
>     self.button["command"] = self.getPath(t)
>
> will call self.getPath(t) and then assign the return value to button["command"].
> quite obvious, if you think about it, right ?
>
> the easiest way to fix this is to add a local function that calls getPath, and
> use that as the button callback:
>
>     def callback():
>         self.getPath(t)
>     self.button["command"] = callback
>
> (if you want, you can inline the getPath code in the callback).
> 
> </F>




More information about the Python-list mailing list