2013年05月28日 星期二 13:20
在wxPython中找到一个用python实现的Splash
代码如下,按照代码逻辑,这个Splash应该在鼠标点击或者3秒钟之后,自动关闭
实际代码中,超过3秒钟也没有反应。
由于对wxPython不太熟悉,麻烦大家帮我找找问题在什么地方。
ps:图片为任意jpg格式图片。“Model.JPG”
class SplashScreen(wx.Frame):
def __init__(self, parent, ID=-1, title="SplashScreen",
style=wx.SIMPLE_BORDER | wx.STAY_ON_TOP | wx.SPLASH_TIMEOUT,
duration=1500, bitmapfile="bitmaps/splashscreen.bmp",
callback=None):
'''
parent, ID, title, style -- see wx.Frame
duration -- milliseconds to display the splash screen
bitmapfile -- absolute or relative pathname to image file
callback -- if specified, is called when timer completes, callback is
responsible for closing the splash screen
'''
### Loading bitmap
self.bitmap = bmp = wx.Image(bitmapfile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
### Determine size of bitmap to size window...
size = (bmp.GetWidth(), bmp.GetHeight())
# size of screen
width = wx.SystemSettings_GetMetric(wx.SYS_SCREEN_X)
height = wx.SystemSettings_GetMetric(wx.SYS_SCREEN_Y)
pos = ((width - size[0]) / 2, (height - size[1]) / 2)
# check for overflow...
if pos[0] < 0:
size = (wx.SystemSettings_GetSystemMetric(wx.SYS_SCREEN_X), size[1])
if pos[1] < 0:
size = (size[0], wx.SystemSettings_GetSystemMetric(wx.SYS_SCREEN_Y))
wx.Frame.__init__(self, parent, ID, title, pos, size, style)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseClick)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBG)
self.Show(True)
print duration
class SplashTimer(wx.Timer):
def __init__(self, targetFunction):
self.Notify = targetFunction
wx.Timer.__init__(self)
if callback is None:
callback = self.OnSplashExitDefault
self.timer = SplashTimer(callback)
# self.Bind(wx.EVT_TIMER, self.OnSplashExitDefault, self.timer)
self.timer.Start(duration, False) # one-shot only
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.DrawBitmap(self.bitmap, 0, 0, False)
def OnEraseBG(self, event):
pass
def OnSplashExitDefault(self, event=None):
print "close my'self by time"
self.Close(True)
def OnCloseWindow(self, event=None):
self.Show(False)
self.timer.Stop()
del self.timer
self.Destroy()
def OnMouseClick(self, event):
self.timer.Notify()
class DemoApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
self.splash = SplashScreen(None, duration=3000, bitmapfile="Model.jpg", callback=self.OnSplashExit)
self.splash.Show(True)
self.SetTopWindow(self.splash)
return True
def OnSplashExit(self, event=None):
print "Yay! Application callback worked!"
self.splash.Close(True)
del self.splash
### Build working windows here...
#----------------------------------------------------------------------
if __name__ == "__main__":
def test(sceneGraph=None):
app = DemoApp(0)
app.MainLoop()
test()
Zeuux © 2024
京ICP备05028076号