使用 time.sleep 更新没有 GUI 的 Wxpython GUI "Freezing"

Using time.sleep to Update Wxpython GUIs without GUI "Freezing"

我使用 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 不显示并且 "freeze." 有人可以帮忙解决这个问题吗?

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