【问题标题】:How to implement multithreading stop button with while loop in python tkinter如何在python tkinter中使用while循环实现多线程停止按钮
【发布时间】:2021-06-16 10:49:03
【问题描述】:

我正在编写用于测量深蹲时力量的应用程序。它从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. 方法:(读取数据是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()))

我在这里寻找一些如何实现停止按钮的示例。

【问题讨论】:

    标签: python multithreading user-interface tkinter


    【解决方案1】:

    在点击中:

        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
    

    【讨论】:

    • 在这种情况下,我在实现 btn 时不需要线程?守护进程正在从 systemd.daemon 导入?
    • stackoverflow.com/questions/4330111/… 好的,非常感谢 :)
    • daemon=True 意味着您不必等待它以 t.join() 结束。
    猜你喜欢
    • 1970-01-01
    • 2021-03-11
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    相关资源
    最近更新 更多