python如何批量执行多个控件的操作?

falloutmx 2018-10-31 01:52:58
我在用tkinter写界面的时候,用到了多个LabelFrame和Entry,需要批量处理他们。一个个写的话觉得太麻烦了,于是想用exec进行批量处理,尝试的方式如下:

zoom = ['zoom0','zoom1','zoom2','zoom3','zoom4'];
zoom0 = LabelFrame(mtk, text="AAA", width=250, height=200)
zoom1 = LabelFrame(mtk, text="BBB", width=250, height=200)
zoom2 = LabelFrame(mtk, text="CCC", width=250, height=200)
zoom3 = LabelFrame(mtk, text="DDD", width=250, height=200)
for i in range(4):
exec("'%s'.pack()"%(zoom[i]))

但是编译的时候报错:
exec("%s.pack()" % (zoom[i]))
File "<string>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'pack'
如果我把(zoom[i])改成(zoom[2])之类的就没有问题,只要是用了i这个参数就会报错。百思不得其解,希望大家能给我解答一下。
...全文
343 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
混沌鳄鱼 2018-10-31
  • 打赏
  • 举报
回复
你贴的代码和报错信息对不上号啊。

代码里面最后一行,%s两边的单引号是多余的。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧