不断刷新 tkinter 消息框中的文本

Refresh text in tkinter message box constantly

我正在自学 python 在我的 raspberry pi 上编码。我正在使用蓝牙 OBD2 扫描仪和此代码来获取响应。我希望消息框继续更新 RPM。我一直在寻找解决方案,但一直无法适应。

import obd
from tkinter import *
connection = obd.Async()
connection.watch(obd.commands.RPM)
connection.start()
master = Tk()
response_RPM=connection.query(obd.commands.RPM)
msg = Message(master, textvariable = connection.query(obd.commands.RPM))
msg.config(bg='lightgreen', font=('times', 24, 'italic'))
msg.pack()
mainloop()

定义回调来处理 RPM 变化,例如update_message_text 并将其作为第二个参数传递给 connection.watch

from tkinter import *

import obd


connection = obd.Async()

master = Tk()
message_text = StringVar()
msg = Message(master, textvariable=message_text)

def update_message_text(rpm, message_text):
  message_text.set(rpm.value)

connection.watch(
    obd.commands.RPM, 
    lambda rpm, message_text=message_text: update_message_text(rpm, message_text)
)
connection.start()

msg.config(bg='lightgreen', font=('times', 24, 'italic'))
msg.pack()

mainloop()