使用'shared_data'方法从不同的帧tkinter访问变量

时间:2018-03-29 13:32:15

标签: python user-interface tkinter file-not-found valueerror

所以这是我在这里提出的问题的延续:How to access user selected file across frames, tkinter

我被引导到这个解决方案:How to access variables from different classes in tkinter python 3

这是我的代码当前的样子,第二页有两个函数试图将所选文件放到第二页上。

    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()

然而,当尝试使用'tk.Entry'方法在Page2中获取变量“filename”时,我遇到错误“。!frame。!page2。!entry”for'print'和“ValueError:文件路径或缓冲区对象类型无效:“for”read_csv()'。

或者,当尝试使用'.get()'方法时,我面临两个实例的错误“FileNotFoundError:File b''不存在”

我理解这些是单个问题的几个错误,但我需要的只是一种工作方法。我不知道为什么上面提到的解决方案不适用于我的代码,因为它是一个几乎完全相同的问题。

任何提示都会有用。

0 个答案:

没有答案