473,379 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Tkinter button not working as expected

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)
frame.pack()

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

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

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

root = Tk()
app = App(root)
root.mainloop()
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?

Sep 21 '06 #1
5 5963
va*********@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.
[...]
what am I doing wrong?
Recently I've done a stupid error with a callback (probably I was
sleeping), so I try again. Note many little changes that I belive
improve your program:

import Tkinter as tk
import tkFileDialog

class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack()

self.entrytxt = tk.StringVar()
entry = tk.Entry(self, textvariable=self.entrytxt)
entry.pack(side=tk.LEFT)

button = tk.Button(self, text="Select", fg="red",
command=self.getPath)
button.pack(side=tk.LEFT)

def getPath(self):
dirPath = tkFileDialog.askdirectory(initialdir="c:/")
self.entrytxt.set(dirPath)
print dirPath

root = tk.Tk()
app = App(root)
root.mainloop()

Bye,
bearophile

Sep 21 '06 #2
va*********@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.

from Tkinter import *
import tkFileDialog

class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

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

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

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

root = Tk()
app = App(root)
root.mainloop()
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?
Well, in python 2.5, you might consider functools.partial.

Other options, including lambda, will be suggested and are the
traditional way to do this sort of thing.

Here is the revised code:

import functools
from Tkinter import *
import tkFileDialog

class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

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

self.button = Button(frame, text="Select", fg="red")

# note change here
self.button["command"] = functools.partial(self.getPath, t)
self.button.pack(side=LEFT)

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

root = Tk()
app = App(root)
root.mainloop()


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Sep 21 '06 #3
va*********@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>

Sep 22 '06 #4
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:
va*********@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>
Sep 22 '06 #5
va*********@yahoo.com wrote:
Thank you Fredrik. It makes total sense now that you've explained it
this way. I frustrated at my ignorance on the assignment issue :)
well, it's one of those "you'll only do this once" mistakes that
*everyone* does (mutable default arguments and unexpected integer
division are two of the others).

it's the "I'm always doing this" mistakes that are really frustrating
(for me, "'str' object has no attribute 'startwith'" is at the top of
that list...)

</F>

Sep 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make....
6
by: Elaine Jackson | last post by:
I've got a script where a button gets pushed over and over: to cut down on the carpal tunnel syndrome I'd like to have the button respond to presses of the Enter key as well as mouse clicks; can...
7
by: Harlin Seritt | last post by:
I was looking at the Tcl/Tk sourceforge page and found that there were a couple of new widgets being produced for Tcl 8.5. Does anyone know if there are any Tkinter wrappers somewhere? thanks, ...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
6
by: JyotiC | last post by:
hi, i am making a GUI using Tkinter, I have a button and a checkbutton. i want the button to be enable when checkbutton is on and disble when the checkbutton is off. thanx
1
by: Kevin Walzer | last post by:
I'm trying to toggle the visibility of a Tkinter widget using pack_forget(), and I'm running into unexpected behavior. The widget "hides" correctly, but does not become visible again. My sample...
1
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
Beginners Game- Tic-Tac-Toe My first attempt after learning Tkinter from Tkinter import * import tkFont class myApp: """ Defining The Geometry of the GUI And the variables Used In the...
8
by: akineko | last post by:
Hi everyone, This is a memorandum so that other people can share the info. The following methods are declared in the Tkinter Button class. tkButtonDown(), tkButtonEnter(), tkButtonInvoke(),...
0
by: Leonhard Vogt | last post by:
Hello I have the following problem in Python 2.5 on Windows XP. On Ubuntu I do not see the problem. I have a Tkinter application as in the following example The entry-widget is somehow...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.