2011-03-01 2 views
1

방금이 이상한 상황에 직면했습니다. 예를 발견했습니다. 여기서 wx.Frame의 OnPaint가 무시되고 원이 그려집니다. Funnily, 하나의 패널을 프레임에 추가하자마자 원은 더 이상 그려지지 않습니다. 사실, OnPaint는 으로 전혀 호출되지 않습니다. 더 이상! (Btw, 나는 Ubuntu Lucid에서 예제를 시도했다.)wxpython : wx.Frame에 패널을 추가하면 wx.Frame의 OnPaint가 비활성화/충돌합니까?

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

답변

1

OK, 이것은 제 생각 엔, 정말 사소한하지 않습니다 ...하지만 나는 그것이 언급 스레드 "wxPython - drawing without paint event - Python answers", 발견

하나의 좋은를 "액션의 wxPython"의 예에서 전략은 인 다음

  • 프레임있다 인 BufferedDC으로 그리는 방법을 그린다 , 우리가 그 예 중 하나를 보면 - 비트 맵 회원에게 체인 , 페인트 방법 내부
  • 는 비트 맵 멤버는

이 ... 그러나, 실제로 약간 오해의 소지가 화면에 그려집니다 Chapter-06/example1.py과 같이 앱이 wx.Frame을 생성한다는 사실이 눈에니다 (예에서와 같이). 여기서 wx.Frame은 간단히 wx.Window을 인스턴스화하여 초기화되며 여기입니다.이 모든 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() 

음을이 사람을 도움이되기를 바랍니다 : 염두에두고

은, 위의 내 코드는 결국 다시 (렌더링 즉, 파란색 원)를 작동하므로 아래와 같이 수정할 수 있습니다!

관련 문제