Mailing List Archive

tkinter global variable
Hello,

I am trying to read a file from askopenfilename outside the function. I
am getting blank file name. What am I doing wrong? Here is my code.

from tkinter import *
from tkinter import filedialog

def on_openfile():
global pic
pic = filedialog.askopenfilename()


root = Tk()

menubar = Menu(root)
root.config(menu=menubar)
file_menu = Menu(menubar)
file_menu.add_command(label="Open", command=on_openfile)
file_menu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="File", menu=file_menu)


f = open(pic)
print(f.read())

root.mainloop()


Thanks.
--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On Fri, 13 Nov 2020 15:41:20 +0000, Stefan Ram wrote:

> ChalaoAdda <chalao.adda@gmail.com> writes:
>>I am trying to read a file from askopenfilename outside the function. I
>>am getting blank file name. What am I doing wrong? Here is my code.
>
> It is possible that you try to read from "pic" before "on_openfile"
> ever was executed.

I didn't get you. I click on the menu item "Open" and then from the
filedialog select a file. And then outside that function I am trying to
read and print the file.
--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On Fri, 13 Nov 2020 16:04:03 +0000, Stefan Ram wrote:

> ChalaoAdda <chalao.adda@gmail.com> writes:
>>On Fri, 13 Nov 2020 15:41:20 +0000, Stefan Ram wrote:
>>> ChalaoAdda <chalao.adda@gmail.com> writes:
>>>>I am trying to read a file from askopenfilename outside the function.
>>>>I am getting blank file name. What am I doing wrong? Here is my code.
>>>It is possible that you try to read from "pic" before "on_openfile"
>>>ever was executed.
>>I didn't get you. I click on the menu item "Open" and then from the
>>filedialog select a file. And then outside that function I am trying to
>>read and print the file.
>
> I have added two additional statements to your source code:
> print( "A" ) and print( "B" ).
>
> If you now execute this new source code, you might observe that "B" is
> being printed before "A" is being printed.
>
> from tkinter import *
> from tkinter import filedialog
>
> def on_openfile():
> print( "A" )
> global pic pic = filedialog.askopenfilename()
>
>
> root = Tk()
>
> menubar = Menu(root)
> root.config(menu=menubar)
> file_menu = Menu(menubar) file_menu.add_command(label="Open",
> command=on_openfile) file_menu.add_command(label="Exit",
> command=root.destroy)
> menubar.add_cascade(label="File", menu=file_menu)
>
>
> print( "B" )
> f = open(pic)
> print(f.read())
>
> root.mainloop()

Ok. I got the point. So what do I need to do access the variable? How do
I return a value from that function?

Thanks.
--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On 11/13/20 11:42 AM, ChalaoAdda wrote:
> On Fri, 13 Nov 2020 16:04:03 +0000, Stefan Ram wrote:
>
>> ChalaoAdda <chalao.adda@gmail.com> writes:
>>> On Fri, 13 Nov 2020 15:41:20 +0000, Stefan Ram wrote:
>>>> ChalaoAdda <chalao.adda@gmail.com> writes:
>>>>> I am trying to read a file from askopenfilename outside the function.
>>>>> I am getting blank file name. What am I doing wrong? Here is my code.
>>>> It is possible that you try to read from "pic" before "on_openfile"
>>>> ever was executed.
>>> I didn't get you. I click on the menu item "Open" and then from the
>>> filedialog select a file. And then outside that function I am trying to
>>> read and print the file.
>> I have added two additional statements to your source code:
>> print( "A" ) and print( "B" ).
>>
>> If you now execute this new source code, you might observe that "B" is
>> being printed before "A" is being printed.
>>
>> from tkinter import *
>> from tkinter import filedialog
>>
>> def on_openfile():
>> print( "A" )
>> global pic pic = filedialog.askopenfilename()
>>
>>
>> root = Tk()
>>
>> menubar = Menu(root)
>> root.config(menu=menubar)
>> file_menu = Menu(menubar) file_menu.add_command(label="Open",
>> command=on_openfile) file_menu.add_command(label="Exit",
>> command=root.destroy)
>> menubar.add_cascade(label="File", menu=file_menu)
>>
>>
>> print( "B" )
>> f = open(pic)
>> print(f.read())
>>
>> root.mainloop()
> Ok. I got the point. So what do I need to do access the variable? How do
> I return a value from that function?
>
> Thanks.

The problem is that you are accessing the variable BEFORE the box has
been put up and the user clicking on it. That doesn't happen until the
mainloop() call. You need to delay the opening and reading of the file
till the filedialog has been used and returned.

Perhaps on_openfile could open and read the file.

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On Fri, 13 Nov 2020 12:04:53 -0500, Richard Damon wrote:

> On 11/13/20 11:42 AM, ChalaoAdda wrote:
>> On Fri, 13 Nov 2020 16:04:03 +0000, Stefan Ram wrote:
>>
>>> ChalaoAdda <chalao.adda@gmail.com> writes:
>>>> On Fri, 13 Nov 2020 15:41:20 +0000, Stefan Ram wrote:
>>>>> ChalaoAdda <chalao.adda@gmail.com> writes:
>>>>>> I am trying to read a file from askopenfilename outside the
>>>>>> function.
>>>>>> I am getting blank file name. What am I doing wrong? Here is my
>>>>>> code.
>>>>> It is possible that you try to read from "pic" before "on_openfile"
>>>>> ever was executed.
>>>> I didn't get you. I click on the menu item "Open" and then from the
>>>> filedialog select a file. And then outside that function I am trying
>>>> to read and print the file.
>>> I have added two additional statements to your source code:
>>> print( "A" ) and print( "B" ).
>>>
>>> If you now execute this new source code, you might observe that "B"
>>> is being printed before "A" is being printed.
>>>
>>> from tkinter import *
>>> from tkinter import filedialog
>>>
>>> def on_openfile():
>>> print( "A" )
>>> global pic pic = filedialog.askopenfilename()
>>>
>>>
>>> root = Tk()
>>>
>>> menubar = Menu(root)
>>> root.config(menu=menubar)
>>> file_menu = Menu(menubar) file_menu.add_command(label="Open",
>>> command=on_openfile) file_menu.add_command(label="Exit",
>>> command=root.destroy)
>>> menubar.add_cascade(label="File", menu=file_menu)
>>>
>>>
>>> print( "B" )
>>> f = open(pic)
>>> print(f.read())
>>>
>>> root.mainloop()
>> Ok. I got the point. So what do I need to do access the variable? How
>> do I return a value from that function?
>>
>> Thanks.
>
> The problem is that you are accessing the variable BEFORE the box has
> been put up and the user clicking on it. That doesn't happen until the
> mainloop() call. You need to delay the opening and reading of the file
> till the filedialog has been used and returned.
>
> Perhaps on_openfile could open and read the file.

Ok. Then how do I access the content of the file from outside the
function? How can I access return value?
Thanks.
--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On Fri, Nov 13, 2020 at 05:12:10PM +0000, ChalaoAdda wrote:
> On Fri, 13 Nov 2020 12:04:53 -0500, Richard Damon wrote:
>
> > On 11/13/20 11:42 AM, ChalaoAdda wrote:
> >> On Fri, 13 Nov 2020 16:04:03 +0000, Stefan Ram wrote:
> >>
> >>> ChalaoAdda <chalao.adda@gmail.com> writes:
> >>>> On Fri, 13 Nov 2020 15:41:20 +0000, Stefan Ram wrote:
> >>>>> ChalaoAdda <chalao.adda@gmail.com> writes:
> >>>>>> I am trying to read a file from askopenfilename outside the
> >>>>>> function.
> >>>>>> I am getting blank file name. What am I doing wrong? Here is my
> >>>>>> code.
> >>>>> It is possible that you try to read from "pic" before "on_openfile"
> >>>>> ever was executed.
> >>>> I didn't get you. I click on the menu item "Open" and then from the
> >>>> filedialog select a file. And then outside that function I am trying
> >>>> to read and print the file.
> >>> I have added two additional statements to your source code:
> >>> print( "A" ) and print( "B" ).
> >>>
> >>> If you now execute this new source code, you might observe that "B"
> >>> is being printed before "A" is being printed.
> >>>
> >>> from tkinter import *
> >>> from tkinter import filedialog
> >>>
> >>> def on_openfile():
> >>> print( "A" )
> >>> global pic pic = filedialog.askopenfilename()
> >>>
> >>>
> >>> root = Tk()
> >>>
> >>> menubar = Menu(root)
> >>> root.config(menu=menubar)
> >>> file_menu = Menu(menubar) file_menu.add_command(label="Open",
> >>> command=on_openfile) file_menu.add_command(label="Exit",
> >>> command=root.destroy)
> >>> menubar.add_cascade(label="File", menu=file_menu)
> >>>
> >>>
> >>> print( "B" )
> >>> f = open(pic)
> >>> print(f.read())
> >>>
> >>> root.mainloop()
> >> Ok. I got the point. So what do I need to do access the variable? How
> >> do I return a value from that function?
> >>
> >> Thanks.
> >
> > The problem is that you are accessing the variable BEFORE the box has
> > been put up and the user clicking on it. That doesn't happen until the
> > mainloop() call. You need to delay the opening and reading of the file
> > till the filedialog has been used and returned.
> >
> > Perhaps on_openfile could open and read the file.
>
> Ok. Then how do I access the content of the file from outside the
> function? How can I access return value?
> Thanks.
What about

def on_printfilename():
global pic
try:
print( f"C: {pic}" )
except NameError:
print( f"C! pic not set yet" )

together with

file_menu.add_command(label="Print filename", command=on_printfilename)

Or move your print("B") block behind the mainloop().

David

--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On 11/13/20 12:12 PM, ChalaoAdda wrote:
> On Fri, 13 Nov 2020 12:04:53 -0500, Richard Damon wrote:
>
>> On 11/13/20 11:42 AM, ChalaoAdda wrote:
>>> On Fri, 13 Nov 2020 16:04:03 +0000, Stefan Ram wrote:
>>>
>>>> ChalaoAdda <chalao.adda@gmail.com> writes:
>>>>> On Fri, 13 Nov 2020 15:41:20 +0000, Stefan Ram wrote:
>>>>>> ChalaoAdda <chalao.adda@gmail.com> writes:
>>>>>>> I am trying to read a file from askopenfilename outside the
>>>>>>> function.
>>>>>>> I am getting blank file name. What am I doing wrong? Here is my
>>>>>>> code.
>>>>>> It is possible that you try to read from "pic" before "on_openfile"
>>>>>> ever was executed.
>>>>> I didn't get you. I click on the menu item "Open" and then from the
>>>>> filedialog select a file. And then outside that function I am trying
>>>>> to read and print the file.
>>>> I have added two additional statements to your source code:
>>>> print( "A" ) and print( "B" ).
>>>>
>>>> If you now execute this new source code, you might observe that "B"
>>>> is being printed before "A" is being printed.
>>>>
>>>> from tkinter import *
>>>> from tkinter import filedialog
>>>>
>>>> def on_openfile():
>>>> print( "A" )
>>>> global pic pic = filedialog.askopenfilename()
>>>>
>>>>
>>>> root = Tk()
>>>>
>>>> menubar = Menu(root)
>>>> root.config(menu=menubar)
>>>> file_menu = Menu(menubar) file_menu.add_command(label="Open",
>>>> command=on_openfile) file_menu.add_command(label="Exit",
>>>> command=root.destroy)
>>>> menubar.add_cascade(label="File", menu=file_menu)
>>>>
>>>>
>>>> print( "B" )
>>>> f = open(pic)
>>>> print(f.read())
>>>>
>>>> root.mainloop()
>>> Ok. I got the point. So what do I need to do access the variable? How
>>> do I return a value from that function?
>>>
>>> Thanks.
>> The problem is that you are accessing the variable BEFORE the box has
>> been put up and the user clicking on it. That doesn't happen until the
>> mainloop() call. You need to delay the opening and reading of the file
>> till the filedialog has been used and returned.
>>
>> Perhaps on_openfile could open and read the file.
> Ok. Then how do I access the content of the file from outside the
> function? How can I access return value?
> Thanks.

When you are using a menuing system like tkinter, you have to think
about program structure differently. Note that YOU never called on_open
in your code (at least not directly), so you can't really get to its
return value. It actually gets called somewhere deep inside mainoop(),
so you could access the value after that, but you won't get there until
you issue the command inside tkinter to shut down the mainloop (your
File / Exit), which is probably later than you want.

Inside interfaces like this, you generally respond to things in the
callbacks.

Now, one other option is that rather than wait for the user to select
the File / Open command, you could just call your on_openfile()
function, which will pop up the dialog, wait for a response, and the set
the answer, then you could use it, but then as I said, that question
comes up before the user does the File / Open command.

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On 13Nov2020 12:48, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
>On Fri, 13 Nov 2020 18:40:53 +0100, David Kolovratník
><david@kolovratnik.net> declaimed the following:
>>def on_printfilename():
>> global pic
>> try:
>> print( f"C: {pic}" )
>> except NameError:
>> print( f"C! pic not set yet" )
>>
>
> Just an aside: you only need "global <name>" if you are modifying the
>value bound to <name>. If <name> does not exist as a local and all one is
>doing is reading <name>, Python will automatically check the module
>(global) scope for that <name>.

This might be confusing< because that state is fragile, only applying if
"pic" is never set, only accessed.

As soon as David modifies on_printfilename() to _assign_ to pic, it
becomes a local name if he hasn't used "global".

IMO, if he must use "pic" as a global it is better to explicitly use
"global" anyway.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: tkinter global variable [ In reply to ]
On Fri, 13 Nov 2020 18:40:53 +0100, David Kolovratník wrote:

> On Fri, Nov 13, 2020 at 05:12:10PM +0000, ChalaoAdda wrote:
>> On Fri, 13 Nov 2020 12:04:53 -0500, Richard Damon wrote:
>>
>> > On 11/13/20 11:42 AM, ChalaoAdda wrote:
>> >> On Fri, 13 Nov 2020 16:04:03 +0000, Stefan Ram wrote:
>> >>
>> >>> ChalaoAdda <chalao.adda@gmail.com> writes:
>> >>>> On Fri, 13 Nov 2020 15:41:20 +0000, Stefan Ram wrote:
>> >>>>> ChalaoAdda <chalao.adda@gmail.com> writes:
>> >>>>>> I am trying to read a file from askopenfilename outside the
>> >>>>>> function.
>> >>>>>> I am getting blank file name. What am I doing wrong? Here is my
>> >>>>>> code.
>> >>>>> It is possible that you try to read from "pic" before
>> >>>>> "on_openfile"
>> >>>>> ever was executed.
>> >>>> I didn't get you. I click on the menu item "Open" and then from
>> >>>> the filedialog select a file. And then outside that function I am
>> >>>> trying to read and print the file.
>> >>> I have added two additional statements to your source code:
>> >>> print( "A" ) and print( "B" ).
>> >>>
>> >>> If you now execute this new source code, you might observe that
>> >>> "B"
>> >>> is being printed before "A" is being printed.
>> >>>
>> >>> from tkinter import *
>> >>> from tkinter import filedialog
>> >>>
>> >>> def on_openfile():
>> >>> print( "A" )
>> >>> global pic pic = filedialog.askopenfilename()
>> >>>
>> >>>
>> >>> root = Tk()
>> >>>
>> >>> menubar = Menu(root)
>> >>> root.config(menu=menubar)
>> >>> file_menu = Menu(menubar) file_menu.add_command(label="Open",
>> >>> command=on_openfile) file_menu.add_command(label="Exit",
>> >>> command=root.destroy)
>> >>> menubar.add_cascade(label="File", menu=file_menu)
>> >>>
>> >>>
>> >>> print( "B" )
>> >>> f = open(pic)
>> >>> print(f.read())
>> >>>
>> >>> root.mainloop()
>> >> Ok. I got the point. So what do I need to do access the variable?
>> >> How do I return a value from that function?
>> >>
>> >> Thanks.
>> >
>> > The problem is that you are accessing the variable BEFORE the box has
>> > been put up and the user clicking on it. That doesn't happen until
>> > the mainloop() call. You need to delay the opening and reading of the
>> > file till the filedialog has been used and returned.
>> >
>> > Perhaps on_openfile could open and read the file.
>>
>> Ok. Then how do I access the content of the file from outside the
>> function? How can I access return value?
>> Thanks.
> What about
>
> def on_printfilename():
> global pic try:
> print( f"C: {pic}" )
> except NameError:
> print( f"C! pic not set yet" )
>
> together with
>
> file_menu.add_command(label="Print filename", command=on_printfilename)
>
> Or move your print("B") block behind the mainloop().
>
> David

Thanks. That solved my problem.

Thanks.
--
https://mail.python.org/mailman/listinfo/python-list