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

时间:2017-11-11 06:11:13

标签: python tkinter refresh obd-ii

我教我自己在我的树莓派上编写python代码。我使用蓝牙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()

1 个答案:

答案 0 :(得分:0)

定义回调以处理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()