Drawing basic shapes
The DeviceContext
API in wxPython provides several methods to draw different kinds of shapes. These primitive shapes can be used as the basis for creating controls, animations, and anything else that you may want to draw on the screen. In this recipe, we will use several DeviceContext
drawing routines in order to paint a little scene on a panel.
How to do it…
Here are the steps that you need to perform:
First, let's set up a panel to use as the canvas for the little scene that we will draw in this recipe, as follows:
import wx class Canvas(wx.Panel): def __init__(self, parent): super(Canvas, self).__init__(parent) self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnBackground)
Now, we will define the
EVT_ERASE_BACKGROUND
handler to fill the background:def OnBackground(self, event): sky = wx.TheColourDatabase.FindColour("SKY BLUE") event.DC.SetBrush(wx.Brush(sky)) event.DC.DrawRectangleRect...