So basically i have a program with 2 frames, where in the first one the user upload a file and in the second one the path of the file is put it on a label. Im using this elegant approach to share data between my frames, but is not working, in my second frame the self.app_data["filePath"] is undefined even after recieve the value of filename variable.
import tkinter as tk
import tkinter.filedialog as filedialog
from tkinter import ttk
LARGE_FONT = ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
## Frame inicial
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Carrefour NLC")
tk.Tk.geometry(self, "500x400")
tk.Tk.resizable(self, 0, 0)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
self.app_data = {"filePath": tk.StringVar()}
for F in (StartPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
return frame
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Upload File", font=LARGE_FONT)
label.pack(pady=20, padx=100)
def fileOpen():
filename = filedialog.askopenfilename()
if filename:
## Setting file path if file exist
print(filename)
self.controller.app_data["filePath"].set(filename)
containerRadio = tk.Frame(self)
buttonFile = ttk.Button(containerRadio, text="Open File",command=lambda:fileOpen())
containerRadio.pack()
buttonFile.pack(side="left")
containerBottom = tk.Frame(self)
button2 = ttk.Button(containerBottom, text="Next",
command=lambda: controller.show_frame(PageOne))
containerBottom.pack(side="bottom", pady=20)
button2.pack(side="left")
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="File Path", font=LARGE_FONT)
## Empty File path
labelPath = tk.Label(self, text=self.controller.app_data["filePath"].get(), font=LARGE_FONT)
label.pack(pady=20, padx=100)
labelPath.pack(pady=20, padx=100)
containerBottom = tk.Frame(self)
button2 = ttk.Button(containerBottom, text="Back",
command=lambda: controller.show_frame(StartPage))
containerBottom.pack(side="bottom", pady=20)
button2.pack(side="left")
app = SeaofBTCapp()
app.mainloop()
i guess the main issue is the self.controller reference somehow is getting lost inside of fileOpen function, i tried to pass a reference of self.controller to the function but still didnt worked.
Thanks in advance, i apreciate any help.
Edit
The answer provided by stovfl textvariable=self.controller.app_data["filePath"]
worked inside of the label, but if i try to set self.controller.app_data["filePath"] to the path variable,path remains undefined.
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
path = self.controller.app_data["filePath"].get()
##Path is empty
print('path:',path)
##textvariable get the value of self.controller.app_data["filepath"]
labelPath = tk.Label(self,
textvariable=self.controller.app_data["filePath"], font=LARGE_FONT)
label.pack(pady=20, padx=100)
labelPath.pack(pady=20, padx=100)