首页 > 解决方案 > 来自 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 上查看我的数据?数据只出现在最后。

谢谢

标签: pythontkinterarduino

解决方案


推荐阅读