TKinter秤和GUI升级版

2024-05-29 09:51:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用tkinter创建一个GUI,其中包含scales按钮等。 现在,我有一套天平。{cdi>可以更新} 现在,我有一个表单列表,例如[[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(scales)没有更新,除了最后一个(在我们的例子中是[3,3,3,3] GUI在执行时被冻结,只有最后一个“运动”反映在缩放值中。在

我是python的初学者,特别是在做gui,如果您能给我一些建议,我将不胜感激


Tags: in元素列表fortkintercountrangegui
1条回答
网友
1楼 · 发布于 2024-05-29 09:51:47

问题是您使用的是一个“for”循环,它阻塞了TK事件循环。这意味着您的程序会计算内容,但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()

相关问题 更多 >

    热门问题