【发布时间】:2011-07-06 21:45:46
【问题描述】:
刚刚遇到这个奇怪的情况:找了个例子,wx.Frame的OnPaint被覆盖,画了一个圆。有趣的是,只要我在框架中添加了一个面板,就不再绘制圆圈了 - 事实上,OnPaint 根本不再被称为 ! (顺便说一句,我在 Ubuntu Lucid 上试过这个例子)
如果这是预期的行为,谁能解释我,如果 wx.Frame 有子面板,如何正确处理 wx.Frame 的OnPaint?小代码示例如下..
提前感谢您的任何回答,
干杯!
代码:
#!/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()
【问题讨论】:
标签: wxpython panel frame draw onpaint