传统知识国际秤和 GUI 更新



我正在尝试使用 tkinter 创建一个包含缩放按钮等的 GUI。现在,我有了这个秤集合。而且我知道比例可以用scale.set()更新现在,我有一个表单列表 [[1,2,3,4,5],[5,4,3,2,1],[3,3,3,3,3]] 例如。

我想遍历列表的每个元素(例如 [1,2,3,4,5](并使用此元素的值更新比例(这也是一个列表(

所以我做

def runMotion():
    #r=3
    for n in range(len(list)):
        print(list[n])
        for count in range(5):
            print(list[n][count])
            motorList[count].scale.set(list[n][count])
            #motorList[count].moveTo(list[n][count])
        time.sleep(5)

这里的 motorList 是一个类数组,每个类都有一个刻度,因此motorList[count].scale

问题是 GUI(比例(没有更新,除了最后一个(在我们的例子中 [3,3,3,3]GUI 在执行时被冻结,只有最后一个"运动"反映在刻度值中。

我是 python 的初学者,特别是做 GUI,我将不胜感激这里的建议

问题是你正在使用一个"for"循环来阻止传统知识事件循环。这意味着事情是由您的程序计算的,但 GUI 不会更新。请尝试以下操作:

list = [[1,2,3,4,5],[5,4,3,2,1],[3,3,3,3,3]]
def runMotion(count):
    if len(list) == count:
        return
    print(list[count])
    for index,n in enumerate(list[count]):
        print(index,n)
        motorList[index].set(n)
        #motorList[count].moveTo(list[n][count])
    root.after(5000, lambda c=count+1: runMotion(c))
root = Tk()
motorList = []
for i in range(1,6):
    s = Scale(root, from_=1, to=5)
    s.grid(row=i-1)
    motorList.append(s)
runMotion(0)
root.mainloop()

最新更新