我遇到了问题,由于无法找到解决方案,请您帮我。 基本上,我试图继承在一个类(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()
答案 0 :(得分:0)
您必须要求Page
实例(controller
)返回PageTwo
实例,并且需要将controller
存储在PageTwo
中构造函数”。
我不知道您可以哈希一个类定义(使用它具有字典的键)。
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()