ori ori - 7 months ago 33
Python Question

wxPython - Showing logo while importing

I am running a wxPython application that does a lot of modules and packages importing at the beginning and gathering information about the computer (network interfaces, internet connection..) so it takes time, and I'm showing a logo in that time until it ends. the problem is that the logo crashes if you press on it (because the importing are on the MainThread and the GUI can't respond to events), How can I show a logo while importing without letting the GUI crash? (I don't want to let the user to click on the Logo anyway)

Answer

Inspired by Tim Roberts in this wxPython-users thread I first tried to spin off the splash screen in a separate thread, which did not work (wxwidgets complains it it not in the main thread). So I did the only obvious thing which I should have done in the first place: make the long running at startup a separate thread.

Side effect: As the splash screens now can react to events, it will disappear when clicked on.

import wx

class long_running(object):
    def __init__(self):
        bitmap = wx.EmptyBitmap(300, 150, 127)

        self.mainframe = wx.Frame(None, -1, 'mainframe')
        self.splash = wx.SplashScreen(bitmap, wx.SPLASH_TIMEOUT, 20000, self.mainframe)


    def start(self):
        import time

        # mimicking something taking very long time on startup
        for i in range(20):
            time.sleep(0.5)
            print i
        wx.CallAfter(self.continue_)

    def continue_(self):
        #Destroy the splash screen.
        if self.splash:
            self.splash.Hide()
#             self.splash.Destroy()

        self.mainframe.Show()


if __name__ == '__main__':
    import thread
    app = wx.App()

    long_rnn = long_running()

    # Begin loading the application.
    thread.start_new_thread(long_rnn.start, ())
    # Application loaded.

    app.MainLoop()