Painting in your UI
All the controls that are visible on screen are created by being painted to DeviceContext. wxPython offers the EVT_PAINT
event to allow user codes to paint their own custom displays on a canvas, such as a window, control, or panel. With this level of control on how the UI is painted, your imagination is the limit; however, in this recipe, we will start simple with just the basics of using EVT_PAINT
and PaintDC
. We will use these features to make a custom panel control that can use bitmaps as its background.
How to do it…
You need to perform the following steps:
First, let's start by defining the custom
Panel
class that will use a bitmap for the background, as follows:class ImageBackground(wx.Panel): def __init__(self, parent, bitmap): super(ImageBackground, self).__init__(parent) self.bitmap = bitmap self.width = bitmap.Size.width self.height = bitmap.Size.height self.Bind(wx.EVT_PAINT, self.OnPaint)
Next, we will define the
OnPaint...