0

I have a UI that presents data from a device. The data is refreshed with a refresh function that loops with a root.after loop.

Sometimes I need to restart the device, in order to do so, i want to

  1. cancel the next root.after loop by a changing a loop variable
  2. send a reset command to the device
  3. Perform an init routine. The last commands of this routine make the loopvariable true, and starts the refresh function.

Problem is that the master after loop is not stopped. Because before the next iteration of the refresh function is started, the reset and initialization are done, and then the refresh function is double fired. Causing the UI to stall.

How can i stop the root.after gracefully?

I've tried to use master.after_cancel, but i could not get it to work.

self.master.after_cancel(loopvar) # loopvar initially declared as None
loopvar = self.master.after(200, self.Updatevalues)

But it resulted in a "tuple index out of range" error. All examples on the net are "plain" examples, not part of a class, and i am struggling getting it to work in the class i've built

Can anyone explain how to fix this? the after_cancel seems the preferred way, but maybe there is a better way.

This is the relevant parts of my current code.

continue_loop = True

class ReadingApp:
    def __init__(self, master):
        self.master = master
        self.TitleFrame = tk.Frame(master, height=80, width=1000, relief='groove', bd=4)
        self.BtnReset = tk.Button(self.F4, text="Restart LCU", command=self.Restart)
        # building rest of UI

    def Restart(self):
        global continue_loop
        continue_loop = False
        LCUreset() # call to external library that resets external device
        self.init_LCU()
    
    def init_LCU
        #perform initialization commands
        continue_loop = True
        self.Updatevalues()

    def Updatevalues(self):
        #read data via modbus
        #update UI labels with new data
        if continue_loop:
            self.master.after(200, self.Updatevalues)
#        else:
#           perform reset from here not possible, because stopping the loop is not always 
#           followed by a reset

C. Bassa
  • 9
  • 2
  • Does this answer your question? [Create a main loop with tkinter?](https://stackoverflow.com/questions/63118430/create-a-main-loop-with-tkinter) – Thingamabobs Jun 04 '21 at 15:08

0 Answers0