我要做的是从arduino序列号读取以下代码,并每隔几秒钟用这些数据更新一个标签。在
当我运行代码时,它只获取/更新一次标签。所以我知道这和循环有关。我的理解是Tk()
和{
from Tkinter import *
import serial
import time
def show_values():
arduinoSerialData.write("55")#Write some data to test Arduino read serial and turn on LED if it does
arduinoSerialData = serial.Serial('/dev/cu.usbmodem1461', 9600, timeout=None)
time.sleep(5) #Arduino Serial Reset Timeout
Joes = Tk()
Joes.wm_title("Read Serial")
myData= arduinoSerialData.readline()
temp = float(myData) #convert string to float store in var
templabel = Label(Joes, text=(temp))
templabel.pack()
c = Button(Joes, text="Send Data", command=show_values)
c.pack()
time.sleep(2)
Joes.mainloop()
看来您误解了TK主循环的工作方式。正如您所描述的,它不是调用
Tk()
和mainloop()
之间的循环,而是在程序代码外部的Tkinter中。在为了有一个循环,更新一个标签,你必须专门编写一个循环,使用Tk的
after
方法,反复调用iterable函数。在您可以创建这样一个函数来执行您想要的操作:
我不确定如何检索arduino数据,因此您可能需要稍微修改一下以获得正确的数据。 这是按照您描述的方式创建循环的一般前提。在
相关问题 更多 >
编程相关推荐