Maciek Groszyk
Maciek Groszyk

Reputation: 71

How to implement multithreading stop button with while loop in python tkinter

I am coding app for measure power during squat. Its taking data from arduino, then there is a method which calculating it on power. This calculations are in while loop. When i press start calculation button my app freeze. I am sure that i have to use Threads here. I was looking for some basic example how to do it, but without any success. This is how its looks atm:

1.Button:

btn = tk.Button(self, text="Zacznij pomiary", command=lambda: methods.clicked(self.txtEntry, self.txtEntry1, self.txtEntry2))
        btn.grid(column=0, row=3)
  1. Method: (read data is a function that making calculation each milisecond in while loop)
def clicked(a, b, c):
    if len(a.get()) == 0 or len(b.get()) == 0 or len(c.get()) == 0:
        popupmsg("Wprowadź poprawne dane!")
    else:
        readData(float(a.get()), float(b.get()), float(c.get()))

I am looking for some examples how to implement stop button here.

Upvotes: 0

Views: 199

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54635

In clicked:

    else:
        global keepGoing
        keepGoing = True
        threading.Thread( target=readData, args=(float(a.get(),float(b.get(),float(c.get()),daemon=True)

In readData:

    while keepGoing:
         ... do stuff ...

Then, make a stop button connected to:

def onStopButton():
    global keepGoing
    keepGoing = False

Upvotes: 1

Related Questions