tkinter global variable

2020-11-13 Thread ChalaoAdda
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

2020-11-13 Thread ChalaoAdda
On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:

> ChalaoAdda  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

2020-11-13 Thread ChalaoAdda
On Fri, 13 Nov 2020 16:04:03 +, Stefan Ram wrote:

> ChalaoAdda  writes:
>>On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
>>> ChalaoAdda  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

2020-11-13 Thread Richard Damon
On 11/13/20 11:42 AM, ChalaoAdda wrote:
> On Fri, 13 Nov 2020 16:04:03 +, Stefan Ram wrote:
>
>> ChalaoAdda  writes:
>>> On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
 ChalaoAdda  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

2020-11-13 Thread ChalaoAdda
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 +, Stefan Ram wrote:
>>
>>> ChalaoAdda  writes:
 On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
> ChalaoAdda  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

2020-11-13 Thread David Kolovratník
On Fri, Nov 13, 2020 at 05:12:10PM +, 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 +, Stefan Ram wrote:
> >>
> >>> ChalaoAdda  writes:
>  On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
> > ChalaoAdda  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

2020-11-13 Thread Richard Damon
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 +, Stefan Ram wrote:
>>>
 ChalaoAdda  writes:
> On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
>> ChalaoAdda  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: How to start using python

2020-11-13 Thread Bob Gailer
I am not feeling well these days. It is sometimes difficult for me to
respond to others the way I would like to.  This is a long reply; in my
humble opinion is important to read all of it

1-whenever you respond to an email from one of us please include the help
list what you can do by reply-all since there are others on this list who
also help.

2 - you did not provide the information I had requested. It's really hard
to help without that information.

3 - problems like you are facing are usually due to some unusual
installation method.

4 - I suspect when you run the Visual Basic icon you enter an interactive
development environment. True? Python also comes with an interactive
development environment its name is IDLE. Idle is normally installed in a
subdirectory how's the one that contains python.exe. once you locate that
director you'll be able to run idle and you will be in an environment that
will look somewhat familiar to you. There are tutorials specifically
written to help you use IDLE. Others on this list can tell you how to
locate that tutorial.

5 - I am sorry you are having such difficulty. It is relatively unusual for
newcomers to experience that.

6 - suggestion: uninstall the thing you installed. go to python.Org. find,
download and run the correct installer paying attention to any information
it gives you on where it is installing python. then try running py again.

On Nov 12, 2020 10:41 PM, "Anthony Steventon" 
wrote:

> I am new to Python and have downloaded the software onto my pc. There is
> no shortcut on my desktop. How the heck do I access it to start learning
> how to program with it?
> Anthony Steventon.
>
> --
> This email has been checked for viruses by AVG.
> https://www.avg.com
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Unable to set up Python correctly

2020-11-13 Thread Charles Okorobo
I have a problem going to one(1) week now. Our instructor told us that we
can check if our Python is well installed or well set up correctly by
typing Control-Shift- P on Windows OS while in VS Code Editor. We cut out
the path enclosed within the Quotation Marks, then we type "Select
Interpreter" and this will auto detect the installation path of our Python
installation. And when we click on that path, it will auto populate the
Path for us.

I did that, and it never happened. I mean that--when I followed all the
instructions, my path never auto populated.

So while the Path of our instructor is 3.8 and 32-bit, mine is 3.9 and
64-bit. But I am not able to auto populate the path to reflect my own path
and Python version. And when I try to print out Hello World, I get an error.

See screenshot of the path error-- https://prnt.sc/vidqif
[image: Mine vs default Path.png]

See screenshot of my Hello World error-- https://prnt.sc/vh6130
[image: Python VS Code error.png]

How can I fix this?

And is anyone willing to use Team Member software to help me remotely fix
this and still show me what you did to fix it?

I've not been able to move forward.
Needing help desperately.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to start using python

2020-11-13 Thread Anthony Steventon
Thanks for the help from everyone.
Operating system is windows 7. Download installation file is 
python-3.7.9-amd64.exe downloaded from python.org. No problems when I run it, 
installation successful.
Have tried 2 + 3 with a result of 5 at the command prompt. whatever I put in 
after that I get a message telling me the entry is undefined.
Anthony Steventon.

From: Bob Gailer 
Sent: Thursday, November 12, 2020 8:58 PM
To: Anthony Steventon 
Cc: python list 
Subject: Re: How to start using python


On Nov 12, 2020 10:41 PM, "Anthony Steventon"  wrote:
>
> I am new to Python and have downloaded the software onto my pc. There is no 
> shortcut on my desktop. How the heck do I access it to start learning how to 
> program with it?

Visit www.Python.Org there should be some links to tutorials that should cover 
the topics of how to install python and how to start using it. If that does not 
help come back to us with more information including your operating system, the 
website from which you downloaded the installer, the name of the installer 
file, and what you did to install python. You also might try from a terminal or 
command prompt typing py, which should start up a python Interactive session. 
You should see>>> type 2 + 3 hit enter you should see a new line displaying 
five. Let us know how it goes and we'll give you a hand from there.

Bob Gailer 


-- 
This email has been checked for viruses by AVG.
https://www.avg.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to start using python

2020-11-13 Thread Cameron Simpson
On 13Nov2020 15:53, Anthony Steventon  wrote:
>Thanks for the help from everyone.
>Operating system is windows 7. Download installation file is 
>python-3.7.9-amd64.exe downloaded from python.org. No problems when I run it, 
>installation successful.
>Have tried 2 + 3 with a result of 5 at the command prompt. whatever I put in 
>after that I get a message telling me the entry is undefined.
>Anthony Steventon.

Please cut/paste the text of what you're trying, along with the full 
error messages. Then we can explain the output to you.

This is a text only list, so no attachments - just paste the text inline 
in your message.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter global variable

2020-11-13 Thread Cameron Simpson
On 13Nov2020 12:48, Dennis Lee Bieber  wrote:
>On Fri, 13 Nov 2020 18:40:53 +0100, David Kolovratník
> 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 " if you are modifying the
>value bound to . If  does not exist as a local and all one is
>doing is reading , Python will automatically check the module
>(global) scope for that .

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 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unable to set up Python correctly

2020-11-13 Thread MRAB

On 2020-11-13 05:28, Charles Okorobo wrote:

I have a problem going to one(1) week now. Our instructor told us that we
can check if our Python is well installed or well set up correctly by
typing Control-Shift- P on Windows OS while in VS Code Editor. We cut out
the path enclosed within the Quotation Marks, then we type "Select
Interpreter" and this will auto detect the installation path of our Python
installation. And when we click on that path, it will auto populate the
Path for us.

I did that, and it never happened. I mean that--when I followed all the
instructions, my path never auto populated.

So while the Path of our instructor is 3.8 and 32-bit, mine is 3.9 and
64-bit. But I am not able to auto populate the path to reflect my own path
and Python version. And when I try to print out Hello World, I get an error.

See screenshot of the path error-- https://prnt.sc/vidqif
[image: Mine vs default Path.png]

See screenshot of my Hello World error-- https://prnt.sc/vh6130
[image: Python VS Code error.png]

How can I fix this?

And is anyone willing to use Team Member software to help me remotely fix
this and still show me what you did to fix it?

I've not been able to move forward.
Needing help desperately.


Have you tried just editing the path in settings.json?

It might be as simple as changing "Python38-32" to "Python39-64", 
assuming that you installed Python in a similar place.

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


Re: tkinter global variable

2020-11-13 Thread ChalaoAdda
On Fri, 13 Nov 2020 18:40:53 +0100, David Kolovratník wrote:

> On Fri, Nov 13, 2020 at 05:12:10PM +, 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 +, Stefan Ram wrote:
>> >>
>> >>> ChalaoAdda  writes:
>>  On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
>> > ChalaoAdda  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