简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM