【问题标题】:Animated Splash in wxPythonwxPython中的动画飞溅
【发布时间】:2016-05-15 19:33:04
【问题描述】:

我在长时间使用 wxPython 和动画 (gif) 飞溅时苦苦挣扎。到目前为止,我有:

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()

我想做这样的事情:

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

任何意见将不胜感激

【问题讨论】:

    标签: python wxpython animated-gif splash-screen


    【解决方案1】:

    你应该在另一个线程上执行耗时的工作,否则 GUI 将阻塞并且没有响应。

    • 让工作线程执行耗时的任务。
    • 完成任务后,通知 GUI 线程以销毁启动画面。

    这是一个sn-p:

    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()
    

    【讨论】:

    • 但是我需要使用函数的返回值。基本上我想让主函数“等待”直到另一个函数完成并在返回值上进一步使用,同时让飞溅旋转。我也尝试在一个单独的线程中启动启动并从他的主线程中调用该函数,但它让我只能在主线程中运行动画。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2020-11-18
    相关资源
    最近更新 更多