简体   繁体   English

如何在 python tkinter 中使用 while 循环实现多线程停止按钮

[英]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.它从arduino中获取数据,然后有一种方法可以计算它的功率。 This calculations are in while loop.此计算在 while 循环中。 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: 1.按钮:

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)方法:(读取数据是一个function,在while循环中每毫秒计算一次)
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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