2
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()  

My second version with theads and after function still doesnt work. data (hour and temperature) don`t appear real-time in the tree-view but it works with print on CLI. How can I view my data on the GUI? Data appears only at the end.

Thanks

Oscar
  • 21
  • 2
  • 1
    Your infinite `while` loop is not allowing Tkinter any chance to process events, such as widget updates. You should use `.after()` to schedule repetitive tasks like this, while still allowing the Tkinter `mainloop()` to run. – jasonharper Sep 28 '19 at 11:17
  • Possible duplicate of [Python - While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Sep 28 '19 at 13:14

0 Answers0