Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Wx NiceButton. By dberube4 aka (Gabriel Dube)
- @Contact : [email protected]
- '''
- import wx
- import wx.lib.newevent
- class NiceButton(wx.Control):
- #Constants
- HIGHLIGHT_NONE = 0
- HIGHLIGHT_BORDER = 1
- HIGHLIGHT_TEXT = 2
- HIGHLIGHT_ALL = 3
- SHAPE_RECTANGLE = 0
- SHAPE_RECTANGLE_ROUNDED = 1
- SHAPE_RECTANGLE_ROUNDED_2 = 2
- SHAPE_RECTANGLE_ROUNDED_3 = 3
- def __init__(self, parent, id = -1, text = '', pos =(0,0) , size = (80, 30)):
- super(NiceButton, self).__init__(parent, id, pos, size, style = wx.NO_BORDER)
- self.SetDoubleBuffered(True)
- #--- Variables ---#
- self.parent = parent
- self.Text = text
- self.Font = wx.Font(9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
- self.palette = {'BackgroundClear': wx.Colour(220, 220, 220),
- 'BackgroundDark': wx.Colour(180, 180, 180),
- 'Foreground': wx.Colour(50, 50, 50),
- 'Highlight': wx.Colour(40, 100, 255),
- 'Shadow' : wx.Colour(140, 140, 140)}
- self.isPressed = False
- self.isHovered = False
- self.HighlightPolicy = self.HIGHLIGHT_BORDER
- self.ShapePolicy = self.SHAPE_RECTANGLE_ROUNDED
- #--- Events ---#
- self.Bind(wx.EVT_PAINT, self.OnPaint)
- self.Bind(wx.EVT_SIZE, self.OnSize)
- self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvent)
- self.Clicked, self.EVT_CLICKED = wx.lib.newevent.NewCommandEvent()
- def OnPaint(self, event):
- dc = wx.PaintDC(self)
- w, h = self.GetSize()
- gc = wx.GraphicsContext.Create(dc)
- if self.isHovered:
- if self.HighlightPolicy == 1 or self.HighlightPolicy == 3:
- gc.SetPen(wx.Pen(self.palette['Highlight'], 1, wx.SOLID))
- else:
- gc.SetPen(wx.Pen(self.palette['Shadow'], 1, wx.SOLID))
- else:
- gc.SetPen(wx.Pen(self.palette['Shadow'], 1, wx.SOLID))
- if self.isPressed:
- brush = gc.CreateLinearGradientBrush(w//2, h, w//2, 0, self.palette['BackgroundClear'], self.palette['BackgroundDark'])
- else:
- brush = gc.CreateLinearGradientBrush(w//2, h, w//2, 0, self.palette['BackgroundDark'], self.palette['BackgroundClear'])
- gc.SetBrush(brush)
- path = gc.CreatePath()
- if self.ShapePolicy == 1:
- path.AddRoundedRectangle(0, 0, w-2, h-2, 5)
- elif self.ShapePolicy == 0:
- path.AddRectangle(0, 0, w-2, h-2)
- elif self.ShapePolicy == 2:
- path.AddRoundedRectangle(0, 0, w-2, h-2, 15)
- elif self.ShapePolicy == 3:
- path.AddRoundedRectangle(0, 0, w-2, h-2, 25)
- gc.DrawPath(path)
- dc.SetFont(self.Font)
- if self.isHovered:
- if self.HighlightPolicy == 2 or self.HighlightPolicy == 3:
- dc.SetTextForeground(self.palette['Highlight'])
- else:
- dc.SetTextForeground(self.palette['Foreground'])
- else:
- dc.SetTextForeground(self.palette['Foreground'])
- x2, y2 = dc.GetTextExtent(self.Text)
- txt = dc.DrawText(self.Text, (w//2)-(x2//2), (h//2)-(y2//2)-1)
- def OnMouseEvent(self, e):
- if e.LeftUp():
- if self.isPressed:
- event = self.Clicked(self.GetId())
- self.GetEventHandler().ProcessEvent(event)
- self.isPressed = False
- self.Refresh()
- elif e.LeftDown():
- self.isPressed = True
- self.Refresh()
- elif e.Entering():
- self.isHovered = True
- self.Refresh()
- elif e.Leaving():
- self.isHovered = False
- self.isPressed = False
- self.Refresh()
- def OnSize(self, event):
- self.Refresh()
- def Clicked(self, event):
- pass
- class Example(wx.Frame):
- def __init__(self, parent, title):
- super(Example, self).__init__(parent, title=title, size=(470, 170))
- self.initUI()
- def initUI(self):
- self.panel = wx.Panel(self)
- #--- Button 1 ---#
- self.btn1 = NiceButton(self.panel, -1, 'Nice Button', wx.Point(10, 10), wx.Size(140, 50))
- #Update Font:
- self.btn1.Font = wx.Font(16, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
- #----------------#
- #--- Button 2 ---#
- self.btn2 = wx.Button(self.panel, -1, 'Wx Button', wx.Point(10, 70), wx.Size(140, 50))
- self.btn2.SetFont(wx.Font(16, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False))
- #----------------#
- #--- Button 3 ---#
- self.btn3 = NiceButton(self.panel, -1, '', wx.Point(160, 10), wx.Size(140, 50))
- #Update Font, Text, Palette, Hightlight:
- self.btn3.Font = wx.Font(16, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
- self.btn3.Text = 'Nice Button'
- self.btn3.HighlightPolicy = 3 #New Highlight policy
- self.btn3.palette['BackgroundClear'] = wx.Color(255, 190, 80)
- self.btn3.palette['BackgroundDark'] = wx.Color(255, 160, 0)
- self.btn3.palette['Foreground'] = wx.Color(180, 100, 0)
- self.btn3.palette['Highlight'] = wx.Color(255, 255, 0)
- self.btn3.palette['Shadow'] = wx.Color(50, 40, 0)
- #----------------#
- #--- Button 4 ---#
- self.btn4 = NiceButton(self.panel, -1, 'Nice Button', wx.Point(160, 70), wx.Size(140, 50))
- #Update Font:
- self.btn4.Font = wx.Font(16, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
- self.btn4.ShapePolicy = NiceButton.SHAPE_RECTANGLE
- #----------------#
- #--- Button 5 ---#
- self.btn5 = NiceButton(self.panel, -1, 'Nice Button', wx.Point(310, 10), wx.Size(140, 110))
- #Update Font:
- self.btn5.Font = wx.Font(16, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
- self.btn5.palette['BackgroundClear'] = wx.Color(255, 130, 180)
- self.btn5.palette['BackgroundDark'] = wx.Color(200, 30, 100)
- self.btn5.palette['Foreground'] = wx.Color(120, 0, 50)
- self.btn5.palette['Highlight'] = wx.Color(255, 0, 0)
- self.btn5.palette['Shadow'] = wx.Color(200, 30, 100)
- self.btn5.ShapePolicy = NiceButton.SHAPE_RECTANGLE_ROUNDED_3
- #----------------#
- self.Show()
- if __name__ == '__main__':
- app = wx.App()
- Example(None, 'Custom widgets example')
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment