簡體   English   中英

如何在 python tkinter 中使用 while 循環實現多線程停止按鈕

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

我正在編寫用於在深蹲期間測量功率的應用程序。 它從arduino中獲取數據,然后有一種方法可以計算它的功率。 此計算在 while 循環中。 當我按下開始計算按鈕時,我的應用程序凍結。 我確信我必須在這里使用線程。 我一直在尋找一些基本示例如何做到這一點,但沒有任何成功。 這是它的外觀:

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. 方法:(讀取數據是一個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()))

我正在尋找一些如何在此處實現停止按鈕的示例。

在點擊:

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

在讀取數據中:

    while keepGoing:
         ... do stuff ...

然后,將停止按鈕連接到:

def onStopButton():
    global keepGoing
    keepGoing = False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM