Python Tkinter在类之间继承DataFrame

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (695)

我遇到了问题,我很乐意请你帮助我,因为我无法找到解决方案。基本上我试图继承加载到一个类(Window1)中的变量的DataFrame,我试图在另一个类(Window1)中使用相同的DataFrame。请在下面找到我目前的简化代码(谢谢):

import tkinter as tk
from PIL import ImageTk, Image
from tkinter import filedialog, messagebox
import time, os, random, string
from datetime import datetime
from time import gmtime, strftime
import pandas as pd

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

        container=tk.Frame(self)
        container.grid()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames={}
        for F in (PageOne, PageTwo, PageThree):
            frame=F(container, self)
            self.frames[F]=frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(PageOne)

    def show_frame(self, cont):
        frame=self.frames[cont]
        frame.tkraise()

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

        start_btn = tk.Button(self, text = "Start >>>", command=lambda:controller.show_frame(PageTwo), width = 10, activebackground = "#ffffff", relief="flat").grid()

        pass

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

        self.browse_btn = tk.Button(self, text="     Browse     ", command=self.select_file)
        self.browse_btn.grid(row=4, column=0, padx=290, pady=10, columnspan=3, sticky="w")

        self.browse_entry = tk.Entry(self, text="", width=30)
        self.browse_entry.grid(row=4, column=0, columnspan=3, padx=100, pady=10, sticky="w")

        self.continue_btn = tk.Button(self, text="Continue >>",  borderwidth=2, width=10, bg="#00c441", fg="#ffffff", command=lambda:[self.print_df(), controller.show_frame(PageThree)])
        self.continue_btn.grid(row=19, column=0, columnspan=3, padx=312, pady=5, sticky="w")
        self.continue_btn.config(state=tk.NORMAL)

    def select_file(self):

        self.path = filedialog.askopenfilename(defaultextension="*.csv", filetypes = (("csv files","*.csv"),("All Files", "*.*")))
        self.browse_entry.delete(0, tk.END)
        self.browse_entry.insert(0, self.path)
        ###following DataFrame I would like to use in in the PageThree class.
        self.df = pd.read_csv(self.path)

    def print_df(self):
        return self.df.head()


class PageThree(PageTwo):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        PageTwo.__init__(self, parent, controller)
        start_btn = tk.Button(self, text = "Data Frame", command=self.funct01, width = 10).grid()

    def funct01(self):
    ##this is where I would like to utilize the DataFrame (inside the function)
        instance=PageTwo(parent,controller)
        print(instance.select_file(self))


if __name__=="__main__":
    app=Page()
    app.geometry("400x400+100+100")
    app.mainloop()
提问于
用户回答回答于

您必须要求Pageinstance(controller)返回PageTwo实例,并且您需要将其存储controllerPageTwo“构造函数”中。

我不知道你可以散列一个类定义(使用它有一个字典的键)。

import tkinter as tk
from PIL import ImageTk, Image
from tkinter import filedialog, messagebox
import time, os, random, string
from datetime import datetime
from time import gmtime, strftime
import pandas as pd

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

        container=tk.Frame(self)
        container.grid()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames={}
        for F in (PageOne, PageTwo, PageThree):
            frame=F(container, self)
            self.frames[F]=frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(PageOne)

    def show_frame(self, cont):
        frame=self.frames[cont]
        frame.tkraise()

    def getFrame(self, frame):
        return self.frames[frame]

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        start_btn = tk.Button(self, text = "Start >>>", command=lambda:controller.show_frame(PageTwo), width = 10, activebackground = "#ffffff", relief="flat").grid()

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

        self.browse_btn = tk.Button(self, text="     Browse     ", command=self.select_file)
        self.browse_btn.grid(row=4, column=0, padx=290, pady=10, columnspan=3, sticky="w")

        self.browse_entry = tk.Entry(self, text="", width=30)
        self.browse_entry.grid(row=4, column=0, columnspan=3, padx=100, pady=10, sticky="w")

        self.continue_btn = tk.Button(self, text="Continue >>",  borderwidth=2, width=10, bg="#00c441", fg="#ffffff", command=lambda:[self.print_df(), controller.show_frame(PageThree)])
        self.continue_btn.grid(row=19, column=0, columnspan=3, padx=312, pady=5, sticky="w")
        self.continue_btn.config(state=tk.NORMAL)

    def select_file(self):

        self.path = filedialog.askopenfilename(defaultextension="*.csv", filetypes = (("csv files","*.csv"),("All Files", "*.*")))
        self.browse_entry.delete(0, tk.END)
        self.browse_entry.insert(0, self.path)
        ###following DataFrame I would like to use in in the PageThree class.
        self.df = pd.read_csv(self.path)

    def print_df(self):
        return self.df.head()

class PageThree(PageTwo):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        PageTwo.__init__(self, parent, controller)
        start_btn = tk.Button(self, text = "Data Frame", command=self.funct01, width = 10).grid()

    def funct01(self):
        instance = self.controller.getFrame(PageTwo)
        dataFrame = instance.df
        print(dataFrame.head())

if __name__=="__main__":
    app=Page()
    app.geometry("400x400+100+100")
    app.mainloop()

扫码关注云+社区

领取腾讯云代金券