如何从不同的线程获取tkinter变量的值?

2024-09-30 01:26:12 发布

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

我有一个tkinter窗口,里面有一些标签。目前,我正在尝试将标签的值设置为我在单独的线程中获取的数据,因为它在while循环中。我已经意识到tkinter不支持从另一个线程设置变量,所以有其他方法可以做到这一点吗?我读了其他一些关于排队的问题,但找不到相关的例子。。。 我的代码:

motorSpeedDisplay = IntVar()

def UpdateVariables():
    while True:
        motorSpeedDisplay.set(int.from_bytes(data, byteorder='little', signed = True))

t1 = threading.Thread(target=updateVariables)
t1.start()

奇怪的是,这工作了一段时间,但随后我添加了具有相同基本代码的其他变量(例如motorCurrentDisplay.ser(int.from_bytes........),然后我开始得到线程错误。如何从该线程获取值并使用它们设置tkinter标签的textvariable值?还有其他方法可以在tkinter窗口中显示数据吗?提前谢谢


Tags: 数据方法代码fromtruebytestkinter标签

热门问题