Python Forum
[Tkinter] tkinter global variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] tkinter global variable
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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()
Reply
#5
(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.
Reply
#6
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.
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,139 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 2,284 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,095 Oct-15-2021, 08:01 AM
Last Post: drSlump
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,546 Mar-10-2021, 04:21 PM
Last Post: Sir
  Global Variable in Event Function? What am I doing wrong here? p_hobbs 1 3,435 Nov-13-2019, 02:50 PM
Last Post: Denni
  [Tkinter] Unable to Access global Variable when switching between frames tziyong 1 3,425 Nov-03-2019, 01:08 AM
Last Post: balenaucigasa
  [Tkinter] Extrakt a Variable from a closed tkinter window hWp 5 3,705 Aug-23-2019, 09:01 PM
Last Post: woooee
  tkinter - global variable not working albry 2 7,015 Jan-26-2019, 04:22 AM
Last Post: Larz60+
  Variable not sharing same value between two different functions Python 2.7 Tkinter albert 0 2,366 Aug-31-2018, 10:45 AM
Last Post: albert
  printing option menu variable in label in Tkinter SmokerX 1 6,591 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020