rom wrote:
Ok. I think I got it. I have to do it in this way:
###########################
import Tkinter
import tkFileDialog
filename=''
root = Tkinter.Tk()
Tkinter.Button(root, text='Notch genes...', command=lambda:
open_file_dialog()).pack()
def open_file_dialog():
global filename
filename = tkFileDialog.askopenfilename(filetypes=
[("allfiles","*")])
print_filename()
def print_filename():
print filename
root.mainloop()
###########################
Thanks again
On Jun 25, 1:46 pm, rom <rompu...@gmail.com> wrote:
Thanks for your response. I have modified this minimal program as you
suggested but still is not able to print the filename:
######################
import Tkinter
import tkFileDialog
global filename
filename=''
root = Tkinter.Tk()
Tkinter.Button(root, text='Notch genes...', command=lambda:
open_file_dialog()).pack()
def open_file_dialog():
filename = tkFileDialog.askopenfilename(filetypes=
[("allfiles","*")])
print filename
root.mainloop()
######################
Is this what you mean?
On Jun 25, 1:28 pm, norseman <norse...@hughes.net> wrote:
OOPS - I left out the global statement
rom wrote:
Hi there,
I am writing an interface with Tkinter. My minimal program looks like
this:
#############
import Tkinter
import tkFileDialog
# define globals here
filename= '' # will take care of the problem
root = Tkinter.Tk()
Tkinter.Button(root, text='Notch genes...', command=lambda:
open_file_dialog()).pack()
def open_file_dialog():
global filename # need this to assign to it
filename = tkFileDialog.askopenfilename(filetypes=[("all
files","*")])
# print filename
root.mainloop()
#############
I would like to recover the filename variable outside the
"open_file_dialog" function. For instance, to be able to print the
selected file name (uncomment "# print filename" line).
Is there a way to do that?
Thanks in advance.
R
===========
Now you got it!! So ignore my just previous response. :)
The global statement inside the def keeps the specified variable from
being local by default. If the global statement is "higher up the food
chain" then it will cause Python to look back for a definition from that
point. There may not be one or it might be other than expected. Python
works backward (or up) from global statement through the defs and
modules enclosing it. Uses first found.
A small suggestion, filename is a word usually used commonly. fname
would be a better substitute. Or fn or ifil, ofil (in/out files) and so
forth. Of course it is perfectly OK for question and answer time.
version: Python
OS : All or Not relevant
date : June 25, 2009
Steve
--
http://mail.python.org/mailman/listinfo/python-list