Magen
Magen

Reputation: 33

Animated Splash in wxPython

I am struggling with wxPython and animated (gif) splash during long taking function. So far I have:

class Splash(wx.SplashScreen):

    def __init__(self, parent=None, id=-1):

        image = "spinner.gif"
        aBitmap = wx.Image(name =image).ConvertToBitmap()
        splashStyle = wx.SPLASH_CENTRE_ON_PARENT
        splashDuration = 0 # milliseconds
        wx.SplashScreen.__init__(self, aBitmap, splashStyle,
                                 splashDuration, parent)

        gif = wx.animate.GIFAnimationCtrl(self, id, image,)

        self.Show()
        self.gif = gif

    def Run(self,):
        self.gif.Play()

I would like to do something like:

splash = Splash()
splash.Run()
result = very_time_consuming_function()
splash.Close()
...
use the result

Any input will be appreciated

Upvotes: 0

Views: 828

Answers (1)

Selçuk Cihan
Selçuk Cihan

Reputation: 2034

You should perform the time consuming work on another thread, otherwise GUI will block and not respond.

  • Have a worker thread perform the time consuming task.
  • Upon completing the task, inform the GUI thread so that the splash is destroyed.

Here is a snippet:

import wx
import wx.animate
from threading import Thread
import time

def wrap_very_time_consuming_function():
    print "sleeping"
    time.sleep(5) # very time consuming function
    print "waking up"
    wx.CallAfter(splash.gif.Stop)
    return 0

app = wx.App()
splash = Splash()
splash.Run()
Thread(target=wrap_very_time_consuming_function).start()
app.MainLoop()

Upvotes: 1

Related Questions