【问题标题】:How to detect if any button is pressed while we are running an endless loop in Python GUI当我们在 Python GUI 中运行无限循环时,如何检测是否按下了任何按钮
【发布时间】:2018-09-25 13:43:16
【问题描述】:

我在 GUI 上有四个具有不同功能的按钮。我像这样构建它们:

button1 = TKinter.Button(btnFrame, text = "bt1", command = bt1func)

我还有一个函数sensor(),一开始必须一直运行,如果有任何按钮被按下,我希望我们能打破调用sensor()的循环并处理按钮的函数。我们如何实现它?谢谢

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    我推荐了这篇文章:tkinter root.mainloop with While True loop

    我们可以使用root.after()来解决这类问题,例如:

    def sensor():
        root.after(200, sensor)
    
    root.after(200, sensor)
    root.mainloop() 
    

    【讨论】:

    • 一个例子而不是一个链接会提高你的回答质量。
    猜你喜欢
    • 2020-11-04
    • 2019-07-18
    • 2015-10-30
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多