我只是遇到了一个奇怪的情况:我找到了一个例子,其中wx.Frame的OnPaint被覆盖,并且画了一个圆圈。有趣的是,一旦我在框架中添加了一个面板,这个圆圈就不再画了--事实上,OnPaint不再被调用了!(顺便说一句,我在Ubuntu Lucid上尝试了这个例子)
有人能解释一下这是否是预期的行为吗?如果OnPaint有子面板,那么如何正确处理wx.Frame的wx.Frame?下面是小代码示例。
谢谢你的回答,
干杯!
守则:
#!/usr/bin/env python
# http://www.linuxquestions.org/questions/programming-9/wxwidgets-wxpython-drawing-problems-with-onpaint-event-703946/
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title, size=wx.DefaultSize):
wx.Frame.__init__(self, parent, wx.ID_ANY, title, wx.DefaultPosition, size)
self.circles = list()
self.displaceX = 30
self.displaceY = 30
circlePos = (self.displaceX, self.displaceY)
self.circles.append(circlePos)
## uncommenting either only first, or both of
## the commands below, causes OnPaint *not* to be called anymore!
#~ self.panel = wx.Panel(self, wx.ID_ANY)
#~ self.mpanelA = wx.Panel(self.panel, -1, size=(200,50))
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, e):
print "OnPaint called"
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen(wx.BLUE))
dc.SetBrush(wx.Brush(wx.BLUE))
# Go through the list of circles to draw all of them
for circle in self.circles:
dc.DrawCircle(circle[0], circle[1], 10)
def main():
app = wx.App()
win = MainWindow(None, "Draw delayed circles", size=(620,460))
win.Show()
app.MainLoop()
if __name__ == "__main__":
main()发布于 2011-03-02 11:32:37
好吧,这不是小事,我想.但我找到了"wxPython -不带画图事件的绘图-“线程,其中提到了:
"wxPython in Action“中一个很好的策略是:
..。然而,这实际上有点误导--如果我们查看其中一个示例,例如第06章/例1.py,可以注意到应用程序生成了一个wx.Frame (在我的示例中是这样的);但是这里的wx.Frame只是通过实例化一个wx.Window来初始化--而在 here 中,所有的DC onPaint都是在这里发生的。
考虑到这一点,可以修改上面的代码,因此它最终可以再次工作(即蓝色圆圈),如下所示:
#!/usr/bin/env python
# http://www.linuxquestions.org/questions/programming-9/wxwidgets-wxpython-drawing-problems-with-onpaint-event-703946/
import wx
class MainWindowWindow(wx.Window):
def __init__(self, parent):
wx.Window.__init__(self, parent)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.circles = list()
self.displaceX = 30
self.displaceY = 30
circlePos = (self.displaceX, self.displaceY)
self.circles.append(circlePos)
## uncommenting either only first, or both of
## the commands below, now calls onPaint
## (without these panels, OnPaint called once - with them, twice)
self.panel = wx.Panel(self, wx.ID_ANY)
self.mpanelA = wx.Panel(self.panel, -1, size=(200,50))
def OnPaint(self, e):
print "OnPaint called"
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen(wx.BLUE))
dc.SetBrush(wx.Brush(wx.BLUE))
# Go through the list of circles to draw all of them
for circle in self.circles:
dc.DrawCircle(circle[0], circle[1], 10)
class MainWindow(wx.Frame):
def __init__(self, parent, title, size=wx.DefaultSize):
wx.Frame.__init__(self, parent, wx.ID_ANY, title, wx.DefaultPosition, size)
MainWindowWindow(self)
def main():
app = wx.App()
win = MainWindow(None, "Draw delayed circles", size=(620,460))
win.Show()
app.MainLoop()
if __name__ == "__main__":
main()希望这能帮上忙,
干杯!
https://stackoverflow.com/questions/5161635
复制相似问题