tkinter:动态标签,显示传感器的值

2024-10-04 07:37:46 发布

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

下面的代码就是我现在的代码。我还用interweb中给定的解决方案进行了尝试,比如这里Link1。在

但这对于像我这样的简单函数来说太复杂了,我希望有更“优雅”的代码。 这就是我所拥有的:

.
.
box = Tk()
box.title('TCS3490')
box.geometry('200x180')

labelLux = Label(master=box, text='Lux=')
labelLux.place(x=5, y=5, width=60, height=30)

labelLuxValue = Label(master=box, text=str(dev.calcLux()))
labelLuxValue.place(x=50, y=50, width=100, height=30)
labelLuxValue.after(10, labelLuxValue.config(text = str(dev.calcLux()))  

box.mainloop()

它只在标签中放一次值,但不能再放一次。 开发工具calcLux()返回浮点值 为什么?在


Tags: 代码textdevmasterboxplace解决方案width
1条回答
网友
1楼 · 发布于 2024-10-04 07:37:46

代码中的问题是after行只执行一次,您需要一个递归函数:

def update():
    labelLuxValue.config(text = str(dev.calcLux())
    labelLuxValue.after(100, update)

update() 

示例见: https://mail.python.org/pipermail/tkinter-discuss/2006-April/000704.html

相关问题 更多 >