Tkinter button not working as expected

Fredrik Lundh fredrik at pythonware.com
Fri Sep 22 05:53:10 EDT 2006


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