473,394 Members | 1,800 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,394 software developers and data experts.

tkFileDialog with tKinter

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:
---
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.     def openfile(self,n):
  26.         print "Okay"
  27.         #More will go here
  28.  
  29.  
  30. root = Tk()
  31.  
  32. app = App(root)
  33.  
  34. 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.
Feb 15 '12 #1
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:
Expand|Select|Wrap|Line Numbers
  1.             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.
Feb 15 '12 #2

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

Similar topics

1
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...
2
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...
5
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. ...
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...
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...
4
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...
2
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...
1
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...
0
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 =...
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...
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: 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: 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
0
BarryA
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...
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
marktang
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,...
0
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...
0
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...

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.