【问题标题】:Using time.sleep to Update Wxpython GUIs without GUI "Freezing" [duplicate]使用 time.sleep 在没有 GUI“冻结”的情况下更新 Wxpython GUI [重复]
【发布时间】:2015-04-24 03:02:45
【问题描述】:

我使用 wxpython 编写了三个独立的 GUI。

def __init__(self, app):
    self.gui1= Gui1(None)
    self.gui1.Show()
    self.gui2= Gui2(None)
    self.gui2.Show()
    self.run_game()

def run_game(self):
    for i in range(0, 100):
        time.sleep(0.5)
        self.gui1.method1()
        self.gui2.method1()

if __name__ == '__main__':
    app = wx.App(False)
    controller = Controller(app)
    app.MainLoop()

但是,当我运行此代码时,GUI 不显示并且似乎“冻结”。有人可以帮忙吗?

【问题讨论】:

  • 没有看到什么方法1,我会说这是由于 100 x 0.5 秒的循环,在此期间 UI 不会更新。

标签: python-2.7 user-interface wxpython


【解决方案1】:

不要使用time.sleep。它阻塞了主循环。相反,请使用wx.Timer()。它们在自己的非阻塞事件循环中运行。您可以阅读文档here 或查看以下教程:

【讨论】:

    猜你喜欢
    • 2010-10-08
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多