Tkinter button not working as expected

vagrantbrad at vagrantbrad at
Thu Sep 21 23:28:31 CEST 2006

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.

from Tkinter import *
import tkFileDialog

class App:

    def __init__(self, master):

        frame = Frame(master)

        t = StringVar()
        self.entry = Entry(frame, textvariable=t)

        self.button = Button(frame, text="Select", fg="red")
        self.button["command"] = self.getPath(t)

    def getPath(self, t):
        dirPath = tkFileDialog.askdirectory(initialdir="c:\\")
        print dirPath

root = Tk()
app = App(root)

The problem arises when I try to pass the t variable in the "command"
option of the button widget.  If I set the command to
"command=self.getpath" rather than "command=self.getpath(t)", then I
don't get this issue.  But I would like to be able to pass a variable
to the the function through the command option.  I'm a Tk newbie ....
what am I doing wrong?

More information about the Python-list mailing list