I am trying to make a simple app to select and operate on data. I am using Tkinter to build a GUI with four labels and four buttons, side by side. However, for some reason when I run the script, all the button commands get called, asking the user to select all files upon startup, and then afterwards the buttons do not work. Here's my code:
--- -
import numpy
-
import scipy
-
-
from Tkinter import *
-
import tkFileDialog as tkf
-
-
filenames = {}
-
-
class App:
-
def __init__(self, master):
-
-
frame = Frame(master)
-
frame.pack()
-
-
self.Name0 = Label(frame, text = "Initial Data", width=30).grid(row=0, sticky=W,columnspan=2)
-
self.Name1 = Label(frame, text = "After Change #1", width=30).grid(row=1, sticky= W,columnspan=2)
-
self.Name2 = Label(frame, text = "After Change #2", width=30).grid(row=2,sticky=W,columnspan=2)
-
self.Name3 = Label(frame, text = "Current State", width=30).grid(row=3,sticky=W,columnspan=2)
-
-
self.Butt0 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=0,column=2)
-
self.Butt1 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=1,column=2)
-
self.Butt2 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=2,column=2)
-
self.Butt3 = Button(frame, text = "Select", command=tkf.askopenfile()).grid(row=3,column=2)
-
-
def openfile(self,n):
-
print "Okay"
-
#More will go here
-
-
-
root = Tk()
-
-
app = App(root)
-
-
root.mainloop()
----
The funny things is that if I remove the dependence on n from the "openfile" method, it works. But obviously, I want to import four distinct sets of data, so I need the dependence on n. This makes no sense! Please help.
1 2113 bvdet 2,851
Expert Mod 2GB
Your button "commands" are being executed as the code is interpreted. "command" needs to be set to a function, not what a function returns. I might initialize 4 attributes to contain the file names and assign "command" similar to this: - self.Butt0 = Button(frame, text = "Select", command=lambda: self.ask_file_name(self.fn1)).grid(row=0,column=2)
You would need a method "ask_file_name" that would accept the variable for assignment of the file name.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mike Abel |
last post by:
Hello
i have a Linux Mandrake Distribution with Python 2.3 and Tcl/Tk 8.4.
Python 2.2 and Tcl/Tk 8.3.3 returns an String if i made this:
x = tkFileDialog.askdirectory(initialdir="was auch...
|
by: Irmen de Jong |
last post by:
Hi,
I'm having trouble with the code below.
It's just a regular Tk text widget in which you can type and
select text as expected, however the call to tkFileDialog.askopenfilename()
seems to screw...
|
by: max(01)* |
last post by:
hello.
i wrote a very simple tkinter demo program that uses menus, buttons,
labels, entries, frames and secondary toplevels.
it is a python version of a java program made by a colleague.
...
|
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...
|
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...
|
by: mdmdmd |
last post by:
Hello,
I wish to collect 4 files from a user. So I have decided to use
tkFileDialog askopenfilename. My problem is that after a few file
selections the root window is destroyed (the whole...
|
by: Traclo |
last post by:
Hello all! I have a problem concerning tkFileDialog. When I use the the askopenfilename command to open a text file it opens a tkinter window (apart from the file browsing window) which refuses to...
|
by: alivip |
last post by:
I integrat program to be GUI using Tkinter I try browser direction
as you can see
# a look at the Tkinter Text widget
# use ctrl+c to copy, ctrl+x to cut selected text,
# ctrl+v to...
|
by: Matthias Huening |
last post by:
Hi,
I have problems using tkFileDialog under Linux (Ubuntu 8.04 in my case, but
other Linuxes seem to show the same behaviour).
The following works fine:
import tkFileDialog
f =...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
| |