Created
July 14, 2014 22:20
-
-
Save JonasPf/7e76a35bd7817a2f5690 to your computer and use it in GitHub Desktop.
Custom wxPython control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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