tkinter: get filename of askopenfilename

R

rom

Hi there,

I am writing an interface with Tkinter. My minimal program looks like
this:
#############
import Tkinter
import tkFileDialog

root = Tkinter.Tk()

Tkinter.Button(root, text='Notch genes...', command=lambda:
open_file_dialog()).pack()

def open_file_dialog():
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
 
N

norseman

rom said:
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():
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
 
N

norseman

OOPS - I left out the global statement

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
 
R

rom

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?


OOPS - I left out the global statement
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

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

rom

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

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?

OOPS - I left out the global statement
# define globals here
filename= ''     # will take care of the problem
       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
 
S

Sean McIlroy

i think what he means is to put the global declaration inside the
function that assigns to filename:

def open_file_dialog():
global filename
filename = tkFileDialog.askopenfilename(filetypes=
[("allfiles","*")])

as it was, the function was creating a new variable called filename
and assigning to THAT (and then doing absolutely nothing with it).
with the above modification, the function understands that filename
refers to the global variable of that name, and that variable's value
does indeed get printed, but since the print statement comes before
root.mainloop() -- hence before the button gets pressed -- filename
gets printed before the function has assigned to it. this fact becomes
apparent if you initialize the variable with filename='blank' (for
example). putting the print statement after root.mainloop() doesn't
work either, since root.mainloop() keeps control from getting to the
print statement. the effect i think you want can be gotten from
putting the print statement into the function as well, so what you end
up with is this:

import Tkinter
import tkFileDialog

filename = 'uninitialized'

def open_file_dialog():
global filename
filename = tkFileDialog.askopenfilename(filetypes=
[("allfiles","*")])
print filename

root = Tkinter.Tk()
Tkinter.Button(root, text='Notch genes...',
command=open_file_dialog).pack()
root.mainloop()
 
R

rom

Thanks again. After your replies, I have understood how to do what I
wanted. What I wanted to do is to get a value after clicking a button
and use it in another part of the program. As you said, after getting
the value, I have to store it in a global variable. However, the
program does not do anything with it until I trigger another event,
e.g. by clicking on another button. Therefore, I have added another
button to my program:
#####################
import Tkinter
import tkFileDialog

filename = 'uninitialized'

def open_file_dialog():
global filename
filename = tkFileDialog.askopenfilename(filetypes=
[("allfiles","*")])

def print_variable(variable):
print variable

root = Tkinter.Tk()
Tkinter.Button(root, text='Select file...',
command=open_file_dialog).pack()

Tkinter.Button(root, text='Print file', command=lambda: print_variable
(filename)).pack()

root.mainloop()
#####################
 
N

norseman

rom said:
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

# NO NO NO! No global line here
filename=''

root = Tkinter.Tk()

Tkinter.Button(root, text='Notch genes...', command=lambda:
open_file_dialog()).pack()

def open_file_dialog():

# global var goes here, inside the def so the var filename is not local
# just like I show below.
filename = tkFileDialog.askopenfilename(filetypes=
[("allfiles","*")])


print filename

root.mainloop()
######################

Is this what you mean?


OOPS - I left out the global statement
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
 
N

norseman

rom said:
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

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?

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
 
N

norseman

rom said:
Thanks again. After your replies, I have understood how to do what I
wanted. What I wanted to do is to get a value after clicking a button
and use it in another part of the program. As you said, after getting
the value, I have to store it in a global variable. However, the
program does not do anything with it until I trigger another event,
e.g. by clicking on another button. Therefore, I have added another
button to my program:
#####################
import Tkinter
import tkFileDialog

filename = 'uninitialized'

def open_file_dialog():
global filename
filename = tkFileDialog.askopenfilename(filetypes=
[("allfiles","*")])

def print_variable(variable):
print variable

root = Tkinter.Tk()
Tkinter.Button(root, text='Select file...',
command=open_file_dialog).pack()

Tkinter.Button(root, text='Print file', command=lambda: print_variable
(filename)).pack()

root.mainloop()
#####################

Your original, as written, would open and print immediately. But it
could be a problem as (or if) more code is added in that area.
This current code allows the user to open, perhaps view via other code
and print only if wanted.

Either is OK, having both is OK, making it do what Rom wants is best. :)

If open/print was wanted, put the print statement in the open file
button group right after the open file statement. Anyone reading the
code will immediately understand that is the intent. At least for that
button.

Also it would be best to put the def statements before the button
statements. Actually it is best to put all defs BEFORE the GUI (frame,
buttons, etc...) statements when using Tkinter.

The print filename statement being placed as in the original request may
attempt to print filename after other code above it is run. Since
Tkinter is not a 'closed' package, that is, it's statements can be
interspersed with program code, it is thus best to make sure what will
and will not be triggered by other actions. I've beat my head on that a
few times. :)


Steve


....(snip)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top