Tkinter小部件刷新移动到错误的框架

时间:2020-10-08 14:04:57

标签: python user-interface tkinter

通过以下代码,我能够通过简单地销毁并重新创建它来更新具有数据框列的列表框。但是,问题在于,当我创建新的列表框时,它是在类A的框架中创建的,而不是在我希望的类别B中创建的,那么如何在类B中定义的框架中创建新的列表框呢?

class MainApplication(tk.Tk):
    def __init__(self, master, *args, **kwargs):
        tk.Frame.__init__(self, master)
        self.master = master
        self.shared_data = {"listbox1": []}
        self.container = tk.Frame(self)
        self.container.pack(side="top", fill="both", expand=True)
        self.container.grid_rowconfigure(0, weight=1)
        self.container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        self.frames["A"] = A(master=self.container, controller=self)
        self.frames["B"] = B(master=self.container, controller=self)

        self.top_frame = Frame(master)
        self.top_frame.pack(side=TOP, expand=True)
        self.bottom_frame = Frame(master)
        self.bottom_frame.pack(side=BOTTOM, expand=True)

        self.left_frame = Frame(self.bottom_frame)
        self.left_frame.pack(side=LEFT, expand=True)

        self.right_frame = Frame(self.bottom_frame)
        self.right_frame.pack(side=RIGHT, expand=True)

        self.A = A(self.top_frame, controller=self)
        self.B = B(self.left_frame, controller=self)

    def get_page(self, c):
        return self.frames[c]

class A(tk.Frame):
    def __init__(self, master, controller):
        tk.Frame.__init__(self, master)
        self.master = master
        self.controller = controller
        self.Buttonfont = tkFont.Font(family="arial", size=15, weight=tkFont.BOLD)
        self.frame = Frame(self.master)
        self.frame.pack()
        self.master.openfile = Button(self.frame, text="Open File", font=self.Buttonfont, command=lambda:[self.import_dataframe(), B.refresh()])
        self.master.openfile.pack(side=TOP)

    def import_dataframe(self):
        #with pandas:
        #self.filename = filedialog.askopenfilename(initialdir="/", title="Select File", filetypes=(("Comma Separated Values", "*.csv"), ("Excelsheet","*.xlsx")))
        #df = pd.read_csv(self.filename, index_col = 0)
        #self.controller.shared_data["listbox1"] = df.columns.to_list()
        #without pandas:
        self.controller.shared_data["listbox1"] = ["item1", "item2"]

class B(tk.Frame):
    def __init__(self, master, controller):
        tk.Frame.__init__(self, master)
        self.master = master

        self.controller = controller
        self.listframe = Frame(self.master)
        self.listframe.pack(side=TOP, fill="both")

        self.listbox = Listbox(self.listframe, height=30, width=50)
        self.listbox.pack(side=TOP)
        for i in self.controller.shared_data["listbox1"]:
            self.listbox.insert(END, i)

        def refresh(self):
            self.listbox.destroy()
            self.listbox = Listbox(self.listframe, height=30, width=50)
            self.listbox.pack(side=TOP)
            for i in self.controller.shared_data["listbox1"]:
                self.listbox.insert(END, i)

1 个答案:

答案 0 :(得分:0)

我找到了它 答案一直盯着我。我需要使用控制器来引用B类,否则只是假设我仍在A类中。下面的完整代码:

[error] Oct 08, 2020 10:50:19 AM org.neo4j.driver.internal.logging.JULogger info
[error] INFO: Direct driver instance 1881901842 created for server address localhost:7687
[error] Oct 08, 2020 10:50:19 AM org.neo4j.driver.internal.logging.JULogger info
[error] INFO: Closing driver instance 1881901842
[error] Oct 08, 2020 10:50:19 AM org.neo4j.driver.internal.logging.JULogger info
[error] INFO: Closing connection pool towards localhost:7687