Python Forum
[WxPython] How to deal with code that blocks the mainloop, freezing the gui
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[WxPython] How to deal with code that blocks the mainloop, freezing the gui
#2
If you just want to delay something, the first argument to after is how long to wait in millisecs before calling the passed in callback.
import wx


class MainFrame(wx.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.panel = wx.Panel(self)
        self.panel_sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel.SetSizer(self.panel_sizer)
        self.label = wx.StaticText(self.panel, label='Player 1 turn')
        self.panel_sizer.Add(self.label, 0, wx.ALL | wx.CENTER, 5)
        self.button = wx.Button(self.panel, label='Player 1 Move')
        self.button.Bind(wx.EVT_BUTTON, self.on_button)
        self.panel_sizer.Add(self.button, 0, wx.ALL | wx.CENTER, 5)
        self.Layout()
        self.Show()

    def on_button(self, event):
        print('Button clicked')
        self.label.SetLabel('player 2 thinking')
        self.button.Disable()
        wx.CallLater(3000, self.delayed_player_2)

    def delayed_player_2(self):
        self.label.SetLabel('player 2 moving')
        wx.CallLater(1000, self.player_2_finsihed)

    def player_2_finsihed(self):
        self.label.SetLabel('player 1 turn')
        self.button.Enable()


if __name__ == '__main__':
    app = wx.App(False)
    main_frame = MainFrame(None)
    app.MainLoop()
Reply


Messages In This Thread
RE: How to deal with code that blocks the mainloop, freezing the gui - by Yoriz - May-06-2019, 12:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to deal with code that blocks the mainloop, freezing the gui Yoriz 3 30,423 Jun-24-2019, 07:03 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020