如何在Tkinter多窗口中复用相同Frame布局并优化代码组织
Tkinter 多窗口复用布局最优方案
核心思路是抽离通用布局逻辑、封装可变参数,完全避免重复代码,后续新增按钮只需要加1行配置即可。
具体优化步骤
- 第一步:封装四层Frame通用布局生成函数,所有窗口(包括根窗口)都调用该函数生成统一结构,修改布局规则只需要改这一处即可全量生效
- 第二步:封装通用子窗口生成函数,把窗口标题、顶部标题、中间内容文本这三个唯一的差异点做成入参,不需要为每个按钮单独编写窗口函数
- 第三步:根窗口按钮批量生成,维护一个按钮配置列表即可,新增按钮只需要往列表里加对应配置
优化后完整代码
import tkinter as tk from tkinter import messagebox def exitApplication(root): MsgBox = messagebox.askquestion('Exit Application', 'Are you sure you want to exit?', icon='warning') if MsgBox == 'yes': root.destroy() def hide(window): window.withdraw() def show(window): window.deiconify() window.update() # 通用布局生成函数:传入父窗口,返回四层Frame对象 def create_common_layout(parent): topFrm = tk.Frame(parent) topFrm.pack(fill=tk.BOTH) middleFrm = tk.Frame(parent, pady=10, highlightbackground="black", highlightthickness=1) middleFrm.pack(fill=tk.BOTH) bottomFrm = tk.Frame(parent) bottomFrm.pack(fill=tk.BOTH) endFrm = tk.Frame(parent) endFrm.pack(fill=tk.BOTH) # 统一加版本号 versionLbl = tk.Label(endFrm, text='Version 0.1 2021', font=('Times New Roman', 5)) versionLbl.pack(side=tk.RIGHT) return topFrm, middleFrm, bottomFrm, endFrm # 通用子窗口生成函数 def create_sub_window(root, win_title, top_title, content_text): hide(root) sub_win = tk.Toplevel(root) sub_win.title(win_title) sub_win.geometry('') # 调用通用布局 topFrm, middleFrm, bottomFrm, _ = create_common_layout(sub_win) # 填充自定义内容 tk.Label(topFrm, text=top_title).pack() tk.Label(middleFrm, text=content_text).grid(row=0, columnspan=3) tk.Button(bottomFrm, text='Back to Home', command=lambda:[show(root), sub_win.destroy()]).pack() sub_win.protocol("WM_DELETE_WINDOW", lambda: exitApplication(root)) if __name__ == "__main__": root = tk.Tk() root.geometry('') root.resizable(False, False) root.protocol("WM_DELETE_WINDOW", lambda: exitApplication(root)) # 根窗口调用通用布局 topFrm, middleFrm, bottomFrm, _ = create_common_layout(root) # 填充根窗口内容 tk.Label(topFrm, text='Homepage').pack() tk.Label(middleFrm, text='Choose one of the following options.\n A new window will open.').grid(row=0, columnspan=4) # 按钮配置列表:后续新增按钮只需要在这里加对应配置即可 button_configs = [ {"text": "Button 1", "win_title": "Button 1", "top_title": "Button 1 Window", "content": "This is the content for Button 1. \n More information will be added in the future.", "col": 0}, {"text": "Button 2", "win_title": "Button 2", "top_title": "Button 2 Window", "content": "This is the content for Button 2. \n This is for Vegetables.\n More Vegetables will be added in the future.", "col": 1}, {"text": "Button 3", "win_title": "Button 3", "top_title": "Button 3 Window", "content": "This is the content for Button 3. \n This is for Fruit\n More Fruit will be added in the future.", "col": 3}, ] # 批量生成按钮 for cfg in button_configs: tk.Button( middleFrm, text=cfg["text"], width=10, height=2, command=lambda c=cfg: create_sub_window(root, c["win_title"], c["top_title"], c["content"]) ).grid(row=1, column=cfg["col"], padx=3, pady=3) # 退出按钮 tk.Button(bottomFrm, text="Exit", width=10, height=1, command=lambda: exitApplication(root)).pack(padx=5, pady=5) root.mainloop()
后续扩展方式
你后续需要新增按钮的时候,只需要在button_configs列表里新增一行配置即可,不需要编写任何冗余的布局代码。如果要调整全局的Frame布局规则,只需要修改create_common_layout函数即可,所有窗口会同步生效。
内容的提问来源于stack exchange,提问作者Gurdish
相关产品推荐
相关产品推荐

