0

I am working on a GUI to display the value from a device. I am able to extract the data from the device but it only happen in single cycle.

I tried the trace() function but it doesn't work. How can i continuously read if the value is being change, and the widget will change automatically.

my code:

def on_variable_trace(*args):
     if entryVariable2.get() == "":
         entryWidget3.configure(state="disable")
     else:
         entryWidget3.configure(state="normal")


D_lbl=Label(window, text="D 2001", fg='red', font=("Helvetica", 16))
D_lbl.place(x=200, y=250,anchor=CENTER)

result_d = IntVar(window,read_D_memory())
D_lbl_r=Label(window, textvariable=result_d, fg='red', font=("Helvetica", 16))
D_lbl_r.place(x=350, y=250,anchor=CENTER)
result_d.trace("w", on_variable_trace)

enter image description here

the value result_d (as shown 301) shall change after i manually change the value from the device

Chun Seong
  • 19
  • 6

1 Answers1

1

I would init the IntVar result_d with zero and then periodically call your read_D_memory() function with the after method. For similar questions look here and here:

from tkinter import Tk, IntVar, Label
from random import randint

def read_D_memory():
    result_d.set(randint(1, 100))  # set result_d to some dummy value
    app.after(100, read_D_memory)  # call function again after 100 ms

app = Tk()
result_d = IntVar(value=0)
test_label = Label(textvariable=result_d)
test_label.pack()

# call function once to init endless loop
app.after(0, read_D_memory)

app.mainloop()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
mnikley
  • 1,625
  • 1
  • 8
  • 21