tkinter: get filename of askopenfilename
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
--
http://mail.python.org/mailman/listinfo/python-list
Re: tkinter: get filename of askopenfilename
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 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
>
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: tkinter: get filename of askopenfilename
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 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 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
>
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: tkinter: get filename of askopenfilename
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()
#
On Jun 25, 4:12 pm, Sean McIlroy wrote:
> 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()
--
http://mail.python.org/mailman/listinfo/python-list
Fw: [wxPython-users] 1>make_buildinfo.obj : error LNK2019: unresolved external symbol [EMAIL PROTECTED] referenced in function _make_buildinfo2
- Forwarded Message From: Josiah Carlson <[EMAIL PROTECTED]> To: f rom <[EMAIL PROTECTED]>; [EMAIL PROTECTED] Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: [wxPython-users] 1>make_buildinfo.obj : error LNK2019: unresolved external symbol [EMAIL PROTECTED] referenced in function _make_buildinfo2 Ask on [email protected] . - Josiah f rom <[EMAIL PROTECTED]> wrote: > I am trying to debug a segfault which I can not pin down with a simple pytjon > script. > For this I have downloaded the free Microsoft visual express c++. > However I am having problems building python2.5. > Anyone have experience with this ? > > 1>-- Rebuild All started: Project: make_buildinfo, Configuration: Debug > Win32 -- > 1>Deleting intermediate and output files for project 'make_buildinfo', > configuration 'Debug|Win32' > 1>Compiling... > 1>make_buildinfo.c > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(43) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(47) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(63) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(66) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(69) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(72) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(73) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(81) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using > strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See > online help for details.' > 1>d:\python-2.5\pcbuild8\make_buildinfo.c(83) : warning C4996: 'strcat' was > declared deprecated > 1>c:\program files\microsoft visual studio 8\vc\include\string.h(78) > : see declaration of 'strcat' > 1>Message: 'This function or variable may be unsafe. Consider using &g
mmmmmmmmmmm
-- http://mail.python.org/mailman/listinfo/python-list
