python - 来自 Arduino 的数据不会出现在树形视图 tkinter 窗口中(Python 3.7)
问题描述
def leertemp():
hora=""
ventanaLectura=Toplevel(root)
ventanaLectura.geometry("630x400")
campos=Frame(ventanaLectura)
campos.pack()
R= Frame(campos, width=500, height=350, bd=8, relief="raise")
R.pack(side=RIGHT)
scrollbary = Scrollbar(R, orient=VERTICAL)
scrollbarx = Scrollbar(R, orient=HORIZONTAL)
tree = ttk.Treeview(R, columns=( "hora","temperatura"), selectmode="extended", height=500, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('hora', text="hora", anchor=W)
tree.heading('temperatura', text="temperatura", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=10)
tree.column('#1', stretch=NO, minwidth=0, width=40)
tree.pack()
tree.delete(*tree.get_children())
def lee():
try:
lectura = serial.Serial('COM3',9600)
lectura.timeout = 1
lectura.setDTR(False)
lectura.flushInput()
lectura.setDTR(True)
sArduino = lectura.readline()
sArduino = sArduino.strip()
sArduino=str(sArduino)
tiempo = time.time()
print (sTemp) # this works fine in real-time
hora=time.strftime("%H:%M:%S")
print(hora) # this works fine in real-time
tree.insert('','end', values=(hora,sTemp)) # doesnt work real-time
except:
messagebox.askquestion("Error")
def read ():
for i in range(5): # trying 5 times
ventanaLectura.after(2000,lee) # reads every 2 seg
thread = threading.Thread(target=read)
thread.start()
ventanaLectura.mainloop()
我的带有theads 和after 功能的第二个版本仍然无法正常工作。数据(小时和温度)不会实时显示在树形视图中,但它适用于 CLI 上的打印。如何在 GUI 上查看我的数据?数据只出现在最后。
谢谢
解决方案
推荐阅读
- point-cloud-library - 限制 PCL 查看器的平移和旋转,但只允许缩放
- json - 如何从子查询中聚合多行?
- azure-data-factory - 文件名上的字符串函数并作为附加列添加
- ember.js - 是否可以使路线模型成为计算/跟踪的属性?
- asp.net - 如何在 ASP.NET ViewBag 中使用 Angular JS 括号来显示 ViewBag.title
- sockets - ZeroMQ 绑定 API zmq_bind() 到 127.0.0.1:5555 返回错误 19
- karate - 检查响应参数中的两种类型
- python - ROS2 RCLPY 库中的速率和睡眠函数
- objective-c - 如何使 Swift 类符合 Objective-C 协议,而不将其暴露给 Objective-C?
- python - 计算精度会给出不稳定的结果