Skip to content

Instantly share code, notes, and snippets.

@JonasPf
Created July 14, 2014 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonasPf/7e76a35bd7817a2f5690 to your computer and use it in GitHub Desktop.
Save JonasPf/7e76a35bd7817a2f5690 to your computer and use it in GitHub Desktop.
Custom wxPython control
import wx
class CustomPanel(wx.PyPanel):
def __init__(self, parent):
wx.PyPanel.__init__(self, parent)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def DoGetBestSize(self):
# tells the sizer the ideal dimensions
return wx.Size(300, 300)
def OnPaint(self, event):
# Double buffer to avoid flickering
dc = wx.BufferedPaintDC(self)
width, height = self.GetClientSize()
backBrush = wx.Brush("red", wx.SOLID)
dc.SetBackground(backBrush)
dc.SetPen(wx.Pen("black"))
dc.SetBrush(wx.Brush("green"))
dc.Clear()
dc.DrawPolygon([
(5,5),
(80,5),
(80,80),
(5,80)
])
dc.DrawRectangle(100,100,150,150)
def OnEraseBackground(self, event):
# Background gets drawn in OnPaint with a double
# buffer to avoid flickering
pass
def main():
app = wx.App()
frame = wx.Frame(None)
custom = CustomPanel(frame)
sizer = wx.BoxSizer()
sizer.Add(custom, 0)
frame.SetSizer(sizer)
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment