0

So this is a continuation of the question that I asked here: How to access user selected file across frames, tkinter

Where I was directed towards the this solution: How to access variables from different classes in tkinter python 3

This is how my code looks currently, there are two functions on page two in my attempts to get the chosen file onto the second page.

    import pandas as pd
    import tkinter as tk
    from tkinter import filedialog

    class GUI(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "GUI")

        self.shared_data = {
                "filename": tk.StringVar(),
            }

        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 = {}

        for F in (Page1, Page2):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(Page1)



    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()



class Page1(tk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self,parent)

        ftypes = [
                ('CSV files','*.csv')
        ]


        def browsefunc2():
            filename = tk.filedialog.askopenfilename(filetypes=ftypes)

            pathlabel.config(text=filename)

            print(filename)


        browsebutton = tk.Button(self, text="Browse", command=browsefunc2, height=1, width=10)
        browsebutton.pack()

        pathlabel = tk.Label(self)
        pathlabel.pack()

        button = tk.Button(self, text="Page 2",
                            command=lambda: controller.show_frame(Page2))
        button.pack()



class Page2(tk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self,parent)



        #method one
        def print_df():
            #print(self.controller.shared_data["filename"].get())
            df = pd.read_csv(self.controller.shared_data["filename"].get())
            print(df)

        #method two
        filename__ = tk.Entry(self, textvariable=self.controller.shared_data["filename"]) 

        def print_df2():
            #print(filename__)
            df = pd.read_csv(filename__)
            print(df)

        button1 = tk.Button(self, text="Print DF 1", command=print_df)
        button1.pack()

        button2 = tk.Button(self, text="Print DF 2", command=print_df2)
        button2.pack()

        button3 = tk.Button(self, text="Page 1",
                            command=lambda: controller.show_frame(Page1))
        button3.pack()

app = GUI()
app.mainloop()

However when trying to use the 'tk.Entry' method to get the variable "filename" in Page2, I am faced with the error ".!frame.!page2.!entry" for 'print' and "ValueError: Invalid file path or buffer object type: " for 'read_csv()'.

Alternatively, when trying to use the '.get()' method, I am faced with the error "FileNotFoundError: File b'' does not exist" for both instances

I appreciate that these are several errors for a single question but all I need is one method to work. I have no idea why the solution mentioned above hasn't worked with my code considering it's an almost identical problem.

Any tips would be useful.

  • At no point do you actually give a value to the shared `filename` var; `browsefunc2()` just sets a local variable of the same name. – jasonharper Mar 29 '18 at 13:56
  • Ok, so how would I assign it to the shared filename? –  Mar 29 '18 at 14:21
  • Call `.set(xxx)` on it, just like you call `.get()` to retrieve the value. – jasonharper Mar 29 '18 at 19:12
  • I'm probably being dumb, but I'm putting the line: `self.controller.shared_data["filename"].set(filename)` within the function in order to `set` it, however I'm just getting the same problem as before –  Mar 29 '18 at 19:32

0 Answers0