0

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)

  • So what exactly goes wrong when you run this? "Not working" is a useless problem description. If there was an error, we need to see the full traceback message. If it just didn't do what you expected, we need to know what you expected. – jasonharper Dec 03 '19 at 14:15
  • 1
    Change to `textvariable=self.controller.app_data["filePath"]` – stovfl Dec 03 '19 at 14:21
  • @jasonharper i just updated my question i hope is more clear now. – Wallace Silva Dec 03 '19 at 14:22
  • @stovfl it worked inside of the label, thank you so much!! but if i try to set the value of filePath to a variable outside of a label dont work, the variable is empty , why is that so ? – Wallace Silva Dec 03 '19 at 14:27
  • @WallaceSilva: ***" the variable is empty"***. You do this `path = self.controller.app_data["filePath"].get()` in `__init__` wich is executed **once** here: `for F in (StartPage, PageOne):`. At this time `tk.StringVar()` is **empty**. – stovfl Dec 03 '19 at 14:51
  • @ stovfl thanks for your support, so on the init of the class,`path = self.controller.app_data["filePath"].get()` is not defined? so what should i do to get the value of filePath where should i define my path variable – Wallace Silva Dec 03 '19 at 14:54

0 Answers0