Search code examples
wxpython

wxPython - Begin and End BusyCursor


Part of my application produces a complex report that take some time to generate. As this may be a few minutes I would like to put up a BusyCursor() to assure people something is happening. The problem I have is that I need to start it in one module and end it in another. This is because it produces a number of reports including a PDF for immediate viewing and another with associated CSV files for subsequent use.


Solution

  • The wxPython have the class wx.StockCursor. This class can create a custom cursor and set into a window area. All cursors is available on: http://www.wxpython.org/docs/api/wx.Cursor-class.html

    You can use this example code:

    import wx
    
    class TestFrame(wx.Frame):
    
        def __init__(self, parent):
            wx.Frame.__init__(self, None)
            self.SetBackgroundColour("#FFFFFF")
            self.rootSizer = wx.BoxSizer(wx.VERTICAL)
    
            self.rootSizer.Add((80, 80)) # Spacer
            self.buttonActive = wx.Button(self,-1,"Activate",size=(100,100))
            self.buttonActive.Bind(wx.EVT_BUTTON, self.OnActivate)
            self.rootSizer.Add(self.buttonActive,0,wx.ALIGN_CENTER)
    
            self.rootSizer.Add((80,80)) # Spacer
            self.buttonDisable = wx.Button(self,-1, "Disable", size=(100,100))
            self.buttonDisable.Bind(wx.EVT_BUTTON, self.OnDisabled)
            self.rootSizer.Add(self.buttonDisable,0,wx.ALIGN_CENTER)
    
            self.SetSizer(self.rootSizer)
    
        def OnActivate(self,evt):
            print "Activate"
            self.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
    
        def OnDisabled(self, evt):
            print "Disabled"
            self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
    
    
    if __name__ == '__main__':
    
        app = wx.App(None)
        frame = TestFrame(None)
        frame.Show()
        app.MainLoop()