I am having a button press call an event, in this event i want to change multiple controls to disabled, run some code then change them back to enabled. This does not seem to work, the buttons only get the self.b1.Enable(False) change after the event has finished. Is there some way to force the UI to update while the event is being called?
You could try, in the event, disable the controls, wx.CallAfter(run_some_code), and then enable the controls again in the end of “run_some_code”.
Or do run_some_code in another process or thread: https://wiki.wxpython.org/LongRunningTasks .
Uhm, there should be no need to go through wx.CallAfter to do what you want… Likely you are just forgetting to Skip the event.
For instance, try this:
import wx, time
class MainFrame(wx.Frame):
def __init__(self, *a, **k):
wx.Frame.__init__(self, *a, **k)
p = wx.Panel(self)
self.b1 = wx.Button(p, -1, 'clic me', pos=(10, 10))
self.b2 = wx.Button(p, pos=(10, 50))
self.b1.Bind(wx.EVT_BUTTON, self.clic)
def clic(self, e):
e.Skip() # try commenting out this line
self.b1.Enable(False)
self.b2.Enable(False)
time.sleep(5) # blocking task
self.b1.Enable(True)
self.b2.Enable(True)
app = wx.App()
MainFrame(None).Show()
app.MainLoop()
Aaaand, that’s what’s happen when you reply late at saturday night… sorry.
Let’s try this one instead:
def clic(self, e):
#e.Skip() # try both ways here... it should be the same but...
self.b1.Enable(False)
self.b2.Enable(False)
wx.Yield()
time.sleep(5)
self.b1.Enable(True)
self.b2.Enable(True)