473,473 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Tkinter Button Commands are Called Only at Startup?

7 New Member
I am trying to use Tkinter to build a simple GUI app which imports data and performs some analysis. The GUI has just four labels and four buttons side by side. When you press the button, you should get a File Dialog prompt. However, whenever I run the script (see below), Python calls all button commands at startup, and then the buttons don't work afterwards. Even if I write the code without a class, the same thing happens. How do I get it to stop doing this? Thanks!

Expand|Select|Wrap|Line Numbers
  1. import numpy
  2. import scipy
  3.  
  4. from Tkinter import *
  5. import tkFileDialog as tkf
  6.  
  7. filenames = {}
  8.  
  9. class App:
  10.     def __init__(self, master):
  11.  
  12.             frame = Frame(master)
  13.             frame.pack()
  14.  
  15.             self.Name0 = Label(frame, text = "Initial Data", width=30).grid(row=0, sticky=W,columnspan=2)
  16.             self.Name1 = Label(frame, text = "After Change #1", width=30).grid(row=1, sticky= W,columnspan=2)
  17.             self.Name2 = Label(frame, text = "After Change #2", width=30).grid(row=2,sticky=W,columnspan=2)
  18.             self.Name3 = Label(frame, text = "Current State", width=30).grid(row=3,sticky=W,columnspan=2)
  19.  
  20.             self.Butt0 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=0,column=2)
  21.             self.Butt1 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=1,column=2)
  22.             self.Butt2 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=2,column=2)
  23.             self.Butt3 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=3,column=2)
  24.  
  25.  
  26. root = Tk()
  27.  
  28. app = App(root)
  29.  
  30. root.mainloop()
  31.  
Feb 15 '12 #1
4 8084
dwblas
626 Recognized Expert Contributor
Loose the parens in the command statement. Tkinter assumes that it is a function and so adds the parens itself.
Expand|Select|Wrap|Line Numbers
  1.              self.Butt0 = Button(frame, text = "Select", command=tkf.askopenfile).grid(row=0,column=2)
  2.              self.Butt1 = Button(frame, text = "Select", command=tkf.askopenfile).grid(row=1,column=2)
  3.              self.Butt2 = Button(frame, text = "Select", command=tkf.askopenfile).grid(row=2,column=2)
  4.              self.Butt3 = Button(frame, text = "Select", command=tkf.askopenfile).grid(row=3,column=2) 
But then, how do you store the file name selected? You should call a function, which calls askopenfile[name?] and stores the name selected. You can use the same function for all buttons and just send it the button number. Also, grid() returns None so there is no reason to save the button id's.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkFileDialog as tkf
  3. from functools import partial
  4.  
  5. class App:
  6.     def __init__(self, master):
  7.  
  8.              frame = Frame(master)
  9.              frame.pack()
  10.  
  11.              self.Name0 = Label(frame, text = "Initial Data", width=30).grid(row=0, sticky=W,columnspan=2)
  12.              self.Name1 = Label(frame, text = "After Change #1", width=30).grid(row=1, sticky= W,columnspan=2)
  13.              self.Name2 = Label(frame, text = "After Change #2", width=30).grid(row=2,sticky=W,columnspan=2)
  14.              self.Name3 = Label(frame, text = "Current State", width=30).grid(row=3,sticky=W,columnspan=2)
  15.  
  16.              self.Butt0 = Button(frame, text = "Select", command=partial(self.get_file_name, 0)).grid(row=0,column=2)
  17.              print self.Butt0
  18.              Button(frame, text = "Select", command=partial(self.get_file_name, 1)).grid(row=1,column=2)
  19.              Button(frame, text = "Select", command=partial(self.get_file_name, 2)).grid(row=2,column=2)
  20.              Button(frame, text = "Select", command=partial(self.get_file_name, 3)).grid(row=3,column=2)
  21.  
  22.     def get_file_name(self, button_num):
  23.         fname=tkf.askopenfilename()
  24.         print "name for button %d is %s" % (button_num, fname)
  25.  
  26. root = Tk()
  27.  
  28. app = App(root)
  29.  
  30. root.mainloop() 
Feb 15 '12 #2
Daniel M
7 New Member
Oh, wow, that totally worked. Yeah, I plan to store the file names, but I was just testing it. I still don't understand why the parenthesis would cause the call to the button commands immediately and then never again. Anyways, thanks a lot!
Feb 15 '12 #3
dwblas
626 Recognized Expert Contributor
I still don't understand why the parenthesis would cause the call to the button commands
That executes a call to the function and sets the command to the return value of the function, which you could do if you wanted, but I can't think of any reason why anyone would want to.
Feb 18 '12 #4
bvdet
2,851 Recognized Expert Moderator Specialist
You might want to pass a value to the function when the button is pushed. In that case you could use a lambda or closure. Lambda example:
Expand|Select|Wrap|Line Numbers
  1. command=lambda: tkf.askopenfile(file_name_variable1)
Feb 18 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the...
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...
1
by: Elaine Jackson | last post by:
In the script I'm writing, there's a Tkinter Button that contains a square image at some times and is blank at other times. Is there a smart way to make its dimensions stay the same throughout all...
0
by: Casey Hawthorne | last post by:
Is there a better way to delete the image from a Tkinter button other than the following: - reconstructing the button - image=blank.gif -- Regards,
3
by: Shankar Iyer (siyer | last post by:
Hi, I have another Tkinter-related question. At the beginning of my program, a tkinter window is created with six buttons. Each of these buttons is assigned a function that should be executed...
4
by: jodyblau | last post by:
I have search the Groups for help on this, and found some examples of people trying to do something similar to what I am trying to do, but wasn't able to get any of the examples to work. What I...
5
by: vagrantbrad | last post by:
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...
5
by: crystalattice | last post by:
I'm creating a shelve interface using Tkinter. I have a button that allows the user to modify an existing entry; when the button is clicked, a new TopLevel window appears with Entry boxes holding...
0
by: platski | last post by:
I am working on some forms for a button in a spreadsheet. two Button Commands in VB excel. Here are my notes as to what I think will be needed to get the job done: Click NY1TR Send Data...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.