首页 > 解决方案 > 在不同的文件上调用 Tkinter GUI 框架类

问题描述

我正在尝试为应用程序构建前端。

我有main.py我的 tkinter 窗口课程。

现在,使用框架,我添加了顶部、左侧和中间框架。

中间框架显示不同的框架,具体取决于在导航栏上单击了哪个按钮。我想将这些帧移动到它们自己的文件中,因为考虑到它们必须显示的所有信息,它们会变得很长。

目前,我的main.py样子是这样的:

import tkinter as tk


class App:

    def __init__(self, root):
        self.root = root
        self.top()
        self.left()
        self.middle()
        self.pageOne()

    def top(self):
        self.top_panel = tk.Frame(self.root)
        self.top_panel.pack()

        self.logo = tk.Label(self.top_panel, text="LOGO GOES HERE", fg="Blue", width=40)
        self.logo.pack(fill=tk.BOTH, expand=1)

    def left(self):
        self.left_frame = tk.Frame(self.root)
        self.left_frame.pack(side="left", fill=tk.BOTH, expand=0)

        self.page_one = tk.Button(self.left_frame, text="Page 1", command=self.pageOne)
        self.page_one.pack(fill=tk.BOTH)
        self.page_two = tk.Button(self.left_frame, text="Page 2", command=self.pageTwo)
        self.page_two.pack(fill=tk.BOTH)
        self.page_three = tk.Button(self.left_frame, text="Page 3", command=self.pageThree)
        self.page_three.pack(fill=tk.BOTH)


    # Middle frame, working area
    def middle(self):
        self.right_frame = tk.Frame(self.root, bg="red", width=60)
        self.right_frame.pack(side="left", fill=tk.BOTH, expand=1)

    # Frames to open on middle frame
    def pageOne(self):
        self.page_one_frame = tk.Frame(self.right_frame, bg="yellow")
        try:
            self.page_two_frame.destroy() or self.page_three_frame.destroy()
        except AttributeError:
            self.page_one_frame.pack(fill=tk.BOTH, expand=1)
        else:
            self.page_one_frame.pack(fill=tk.BOTH, expand=1)

        # pageOne Contents
        self.label = tk.Label(self.page_one_frame, text="This is page one")
        self.label.pack()

    def pageTwo(self):
        self.page_two_frame = tk.Frame(self.right_frame, bg="blue")
        try:
            self.page_one_frame.destroy() or self.page_three_frame.destroy()
        except AttributeError:
            self.page_two_frame.pack(fill=tk.BOTH, expand=1)
        else:
            self.page_two_frame.pack(fill=tk.BOTH, expand=1)

        # pageTwo contents Contents
        self.label = tk.Label(self.page_two_frame, text="This is page two")
        self.label.pack()

    def pageThree(self):
        self.page_three_frame = tk.Frame(self.right_frame, bg="green")
        try:
            self.page_one_frame.destroy() or self.page_two_frame.destroy()
        except AttributeError:
            self.page_three_frame.pack(fill=tk.BOTH, expand=1)
        else:
            self.page_three_frame.pack(fill=tk.BOTH, expand=1)

        # pageThree contents Contents
        self.label = tk.Label(self.page_three_frame, text="This is page three")
        self.label.pack()

root = tk.Tk()
app = App(root)
root.mainloop()

将这些pageOne, pageTwo,pageThree方法“移动”到自己的文件并使用按钮命令调用它们的正确方法是什么?

标签: pythonooptkinterframefile-handling

解决方案


推荐阅读