Tkinter循环配置子Frame网格布局语法错误求助
Tkinter循环配置子Frame网格布局语法错误求助
嘿,我来帮你搞定这个小问题~
你遇到的核心问题很明确:Tkinter的主窗口对象window并不是可迭代的容器,所以直接写for frame in window:不符合Python的语法规则,VSCode的提示就是在告诉你这个写法有问题哦。
要实现批量配置frame的需求,你只需要把需要处理的frame先整理到一个列表里,再遍历这个列表就可以啦。另外我还发现你代码里有个小拼写错误:winsdow.columnconfigure多写了一个d,应该是window.columnconfigure,记得改过来避免后续运行报错。
修正后的完整代码示例如下:
import tkinter as tk # 习惯上用小写tk作为别名更规范哦 window = tk.Tk() window.state('zoomed') window.rowconfigure(0, weight=1, uniform='a') window.columnconfigure(0, weight=1, uniform='a') window_home = tk.Frame(window) window_documents = tk.Frame(window) window_form = tk.Frame(window) # 将需要配置的frame统一放到列表中 target_frames = [window_home, window_documents, window_form] # 遍历列表完成批量配置 for frame in target_frames: frame.rowconfigure(0, weight=1, uniform='a') frame.columnconfigure(0, weight=1, uniform='a')
如果之后你需要添加新的frame,只要把新的frame对象加入到target_frames列表里,循环就能自动处理,非常灵活~
备注:内容来源于stack exchange,提问作者Tarenta
相关产品推荐
相关产品推荐

