问题描述
这是我正在考虑的问题:
here's the issue I'm thinking of:
框架中有许多类似的小部件(例如Lables).我会在算法的每次迭代中更新它们(在相关函数中使用root.update()
.我想知道的是如何遍历每个Label.
I have a number of similar widgets in the frame(e.g. Lables). I update them every iteration of the algorithm (using root.update()
in the relevant function. What I'd like to know, is how to loop through each Label.
当然,它们是用类似的东西创建的
Of course, they were created with something like
self.var1=IntVar()
self.lab1=Label(frame,textvariable=self.var1)
self.lab1.grid()
因此每个标签都被命名为lab1,lab2等.我很确定应该有一种更好的命名方式,这样我就不必显式地调用每个名称,或者以某种方式循环遍历它们
So each of the lables are named lab1,lab2, etc. I'm quite sure there should either be a better way of naming them, so that I don't have to call each name explicitly, or somehow loop though them.
推荐答案
最简单的方法是将对每个窗口小部件的引用存储在列表或字典中,而不是(或除了)scaler实例变量之外.
The easiest way is to store references to each widget in a list or dictionary rather than (or in addition to) scaler instance variables.
这篇关于遍历Tkinter中的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!