python - 在不同的文件上调用 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
方法“移动”到自己的文件并使用按钮命令调用它们的正确方法是什么?
解决方案
推荐阅读
- java - 有没有办法列出 mvn-deploy 来发布工件存储库?
- excel - Excel VBA - 如何将匹配与矩阵一起使用
- html - 关闭时cookie不会自动返回
- python - python循环subprocess.check_output
- odoo-14 - 构建自定义仪表板 Odoo14
- azure - linux 上的 Azure Functions - 不总是可用所以可能空闲的应用程序?
- python - 大熊猫数据框中多种模式的正则表达式
- javascript - 如何在js中找到具有相似选择器的元素?1
- r - 如何根据第三列的值计算两个时间戳列之间的差异?
- mysql - 在nodejs的query()函数中传递多个查询