Tkinter:显示带有自动更新变量的标签
Tkinter: displaying label with automatically updated variable
我知道有很多关于这个主题的问题,但经过长时间的研究,我没有找到任何可以解决我的问题的问题。
我正在尝试使用标签(使用 tkinter)显示从 I²C 总线获取的变量。因此,该变量会定期自动更新。 window 的其余部分应该对用户可用。
目前,我发现显示带有更新变量的标签并保持 window 的其余部分对用户可用的唯一方法是:
window = tk.Tk()
window.title("Gestionnaire de périphériques")
window.minsize(1024,600)
labelValThermo = tk.Label(a_frame_in_the_main_window,text = "")
labelValThermo.grid(row = 1, column = 1)
while True:
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
window.update()
time.sleep(0.75)
来自 I²C 并得到更新的变量是 mcp.get_hot_junction_temperature
事实上,我知道这不是在无限循环中强制更新的最佳方式。这应该是mainloop()
的作用。我发现 after()
方法可以解决我的问题,但我不知道如何 运行 它。我尝试了以下无效的代码:
def displayThermoTemp():
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
labelValThermo.after(500,displayThermoTemp)
window = tk.Tk()
labelValThermo = tk.Label(thermoGraphFrame,text = "")
labelValThermo.after(500, displayThermoTemp)
labelValThermo.grid(row = 1, column = 1)
window.mainloop()
有没有人有正确的语法?
如何使用after()
?
after()
在给定的延迟(以毫秒为单位)后调用回调函数。只需在给定函数中定义它,它就会 运行 就像一个 while 循环,直到你调用 after_cancel(id)
。
举个例子:
import tkinter as tk
Count = 1
root = tk.Tk()
label = tk.Label(root, text=Count, font = ('', 30))
label.pack()
def update():
global Count
Count += 1
label['text'] = Count
root.after(100, update)
update()
root.mainloop()
用这个更新你的函数并在mainloop()
之前调用它一次。
def displayThermoTemp():
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
labelValThermo.after(500,displayThermoTemp)
# 100ms = 0.1 secs
window(100, displayThermoTemp)
after 具有以下语法:
after(delay_ms, callback=None, *args)
您需要将命令放在回调函数中并更新小部件所在的 window(在您的情况下,labelValThermo
看起来位于 root
window:
def displayThermoTemp():
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
root.after(500,displayThermoTemp)
我知道有很多关于这个主题的问题,但经过长时间的研究,我没有找到任何可以解决我的问题的问题。
我正在尝试使用标签(使用 tkinter)显示从 I²C 总线获取的变量。因此,该变量会定期自动更新。 window 的其余部分应该对用户可用。
目前,我发现显示带有更新变量的标签并保持 window 的其余部分对用户可用的唯一方法是:
window = tk.Tk()
window.title("Gestionnaire de périphériques")
window.minsize(1024,600)
labelValThermo = tk.Label(a_frame_in_the_main_window,text = "")
labelValThermo.grid(row = 1, column = 1)
while True:
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
window.update()
time.sleep(0.75)
来自 I²C 并得到更新的变量是 mcp.get_hot_junction_temperature
事实上,我知道这不是在无限循环中强制更新的最佳方式。这应该是mainloop()
的作用。我发现 after()
方法可以解决我的问题,但我不知道如何 运行 它。我尝试了以下无效的代码:
def displayThermoTemp():
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
labelValThermo.after(500,displayThermoTemp)
window = tk.Tk()
labelValThermo = tk.Label(thermoGraphFrame,text = "")
labelValThermo.after(500, displayThermoTemp)
labelValThermo.grid(row = 1, column = 1)
window.mainloop()
有没有人有正确的语法?
如何使用after()
?
after()
在给定的延迟(以毫秒为单位)后调用回调函数。只需在给定函数中定义它,它就会 运行 就像一个 while 循环,直到你调用 after_cancel(id)
。
举个例子:
import tkinter as tk
Count = 1
root = tk.Tk()
label = tk.Label(root, text=Count, font = ('', 30))
label.pack()
def update():
global Count
Count += 1
label['text'] = Count
root.after(100, update)
update()
root.mainloop()
用这个更新你的函数并在mainloop()
之前调用它一次。
def displayThermoTemp():
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
labelValThermo.after(500,displayThermoTemp)
# 100ms = 0.1 secs
window(100, displayThermoTemp)
after 具有以下语法:
after(delay_ms, callback=None, *args)
您需要将命令放在回调函数中并更新小部件所在的 window(在您的情况下,labelValThermo
看起来位于 root
window:
def displayThermoTemp():
if mcp.get_hot_junction_temperature() != 16.0625:
labelValThermo.configure(text = "Température thermocouple: {} °C".format(mcp.get_hot_junction_temperature()))
root.after(500,displayThermoTemp)