2009-08-29 49 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 2012-07-03 19:12:36

3

可以擴展默認的按鈕類,像這樣的例子:

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

你把RedButton到您的佈局每一次,它應該出現紅色(沒有測試過,雖然)。

5

當我想了解如何使定製的部件(包括按鈕),我引用Andrea Gavana's page(全在那裏工作的例子)在wxPyWiki和Cody Precord的platebutton(源是wx.lib.platebtn,也是在SVN here) 。看看這兩者,你應該能夠構建你想要的任何自定義小部件。