此代码来自python-forum,它可以在一个简单的幻灯片中查看图像。我觉得它不起作用,因为它创建了图像,但没有在屏幕上显示它们。我想我可能必须实现某种回调,但这是解决我的问题的正确方法吗?如果我摆脱了while循环而只是使用for,它只显示一个图像,而不显示其余图像。
class SecondFrame(wx.Frame):
def __init__(self, parent, mysize):
gets correct path for images from working directory
wx.EVT_PAINT(self, self.onPaint)
def onPaint(self, event=None):
dc = wx.PaintDC(self)
while self.loops > 0:
self.loops -= 1
for self.ix, bmp in enumerate(self.image_list):
w, h = bmp.GetSize()
info = "Graphs"
self.SetTitle(info)
dc.DrawBitmap(bmp, 10, 10, True)
wx.MilliSleep(self.delay)
if self.delay > 200:
dc.Clear()
答案 0 :(得分:1)
我不确定你在哪里获得该代码,但是因为它是不完整的,不可运行的并且整体编码很差(恕我直言)。试试这个片段(未经测试):
class SecondFrame(wx.Frame):
def __init__(self, parent, mysize):
# Whatever here
self.timer = wx.Timer(self, wx.ID_ANY)
self.SetTitle("Graphs")
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.timer.Start(self.delay)
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self)
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
if self.loops <= 0:
return
bmp = self.image_list[self.loops]
w, h = bmp.GetSize()
dc.DrawBitmap(bmp, 10, 10, True)
wx.MilliSleep(self.delay)
def OnErase(self, event):
pass
def OnTimer(self, event):
self.loops -= 1
if self.loops <= 0:
return
self.Refresh()
下次,请按照wxPython指南发布完整,简短,可运行且独立的示例应用程序: