2009-08-29 6 views

답변

8

여기 당신이 완전히 사용자 정의 버튼을 그리는 데 사용할 수있는 골격이다, 보이는 방법 또는 당신의 상상력에 그것의 최대

class MyButton(wx.PyControl): 

    def __init__(self, parent, id, bmp, text, **kwargs): 
     wx.PyControl.__init__(self,parent, id, **kwargs) 

     self.Bind(wx.EVT_LEFT_DOWN, self._onMouseDown) 
     self.Bind(wx.EVT_LEFT_UP, self._onMouseUp) 
     self.Bind(wx.EVT_LEAVE_WINDOW, self._onMouseLeave) 
     self.Bind(wx.EVT_ENTER_WINDOW, self._onMouseEnter) 
     self.Bind(wx.EVT_ERASE_BACKGROUND,self._onEraseBackground) 
     self.Bind(wx.EVT_PAINT,self._onPaint) 

     self._mouseIn = self._mouseDown = False 

    def _onMouseEnter(self, event): 
     self._mouseIn = True 

    def _onMouseLeave(self, event): 
     self._mouseIn = False 

    def _onMouseDown(self, event): 
     self._mouseDown = True 

    def _onMouseUp(self, event): 
     self._mouseDown = False 
     self.sendButtonEvent() 

    def sendButtonEvent(self): 
     event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId()) 
     event.SetInt(0) 
     event.SetEventObject(self) 
     self.GetEventHandler().ProcessEvent(event) 

    def _onEraseBackground(self,event): 
     # reduce flicker 
     pass 

    def _onPaint(self, event): 
     dc = wx.BufferedPaintDC(self) 
     dc.SetFont(self.GetFont()) 
     dc.SetBackground(wx.Brush(self.GetBackgroundColour())) 
     dc.Clear() 
     # draw whatever you want to draw 
     # draw glossy bitmaps e.g. dc.DrawBitmap 
     if self._mouseIn: 
      pass# on mouserover may be draw different bitmap 
     if self._mouseDown: 
      pass # draw different image text 
+0

이 당신을 순전히 감사 동작! 나는 그것을 광범위하게 사용할 것이다! – Mizmor

3

당신은 예를 들어 다음과 같이 기본 버튼 클래스를 확장 할 수

class RedButton(wx.Button): 
    def __init__(self, *a, **k): 
     wx.Button.__init__(self, *a, **k) 
     self.SetBackgroundColour('RED') 
     # more customization here 

당신이 당신의 레이아웃에 RedButton 넣을 때마다, (비록 그것을 테스트하지 않은) 빨간색 표시됩니다.

2

Generic Button 또는 Bitmap Button을 사용해보세요. 나는 사용자 정의 위젯을 만드는 방법을 배우고 싶었다 때

5

는 (버튼 포함) 나는 (소스 또한, wx.lib.platebtn에 SVN에서 here 임) wxPyWiki와 코디 Precord의 platebutton에 Andrea Gavana's page (가 전체 작업 예제를) 참조 . 이 두 가지를 살펴보면 원하는 대부분의 사용자 정의 위젯을 빌드 할 수 있어야합니다.

관련 문제