Python Forum
[Tkinter] tkinter global variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] tkinter global variable (/thread-30925.html)



tkinter global variable - chalao_adda - Nov-13-2020

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.


RE: tkinter global variable - deanhystad - Nov-13-2020

pic is not defined until after you select a file from the file dialog drawn by the function on_openfile() which is called when you select "File" from the file menu. None of that has happened yet when you try to open(pic).

GUI programming is event driven, not linear. Your code responds to actions performed by the user. You got part of this right. You only draw the file dialog when the user has selected "File" from the file menu. You need to use the same paradigm for opening the file. The file dialog needs to call a function when the file is selected. That function is where you will open and read the file.


RE: tkinter global variable - chalao_adda - Nov-13-2020

(Nov-13-2020, 03:20 PM)deanhystad Wrote: pic is not defined until after you select a file from the file dialog drawn by the function on_openfile() which is called when you select "File" from the file menu. None of that has happened yet when you try to open(pic).

GUI programming is event driven, not linear. Your code responds to actions performed by the user. You got part of this right. You only draw the file dialog when the user has selected "File" from the file menu. You need to use the same paradigm for opening the file. The file dialog needs to call a function when the file is selected. That function is where you will open and read the file.

So how do I access the file outside the function? How do I return a value from that function?
Thanks.


RE: tkinter global variable - deanhystad - Nov-13-2020

Forget what I said earlier about the file dialog needing a callback. I am mixing up different GUI toolkits. In tkinter you treat a dialog like it waits for the file to be selected.

I would write the code so you don't access the file outside the on_openfile() function.
from tkinter import *
from tkinter import filedialog
 
def on_openfile():
    pic = filedialog.askopenfilename()
    with open(pic) as f:
        print(f.read())
 
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)
 
root.mainloop()



RE: tkinter global variable - chalao_adda - Nov-14-2020

(Nov-13-2020, 06:19 PM)deanhystad Wrote: Forget what I said earlier about the file dialog needing a callback. I am mixing up different GUI toolkits. In tkinter you treat a dialog like it waits for the file to be selected.

I would write the code so you don't access the file outside the on_openfile() function.
from tkinter import *
from tkinter import filedialog
 
def on_openfile():
    pic = filedialog.askopenfilename()
    with open(pic) as f:
        print(f.read())
 
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)
 
root.mainloop()

Then I would have to write the whole program inside that openfile function. Here is something I could use.

from tkinter import *
from tkinter import filedialog


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

def on_set():
    global file
    e.delete(0,END)
    e.insert(0,file)    

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="Set", command=on_set)

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

e = Entry(root,width=10)
e.pack()

root.mainloop()
Thanks.


RE: tkinter global variable - deanhystad - Nov-14-2020

How is that better? Feels awkward. In GUI programs it is the norm for a menu selection or button press to execute hundreds or thousands of lines of cofe.


RE: tkinter global variable - chalao_adda - Nov-14-2020

(Nov-14-2020, 05:22 AM)deanhystad Wrote: How is that better? Feels awkward. In GUI programs it is the norm for a menu selection or button press to execute hundreds or thousands of lines of cofe.

I am not talking about number of lines of codes. I was trying to access that variable from outside that function. I don't know, there could be simpler way. If I put everything in a class, I could use self.file. Then I guess, I didn't have to use global.

Thanks.