Bitmaps are the basic data type used to represent images in an application. The wx.Bitmap
object can seamlessly load and decompress most common image file formats into a common representation that is usable by many UI controls. Adding bitmaps to the controls can make a UI more intuitive and easier to use.
Perform the following steps:
Let's start this time by making a subclass of
wx.Panel
to use as the container for the control that will show our bitmap on the screen, as follows:class ImagePanel(wx.Panel): def __init__(self, parent): super(ImagePanel, self).__init__(parent) # Load the image data into a Bitmap theBitmap = wx.Bitmap("usingBitmaps.png") # Create a control that can display the # bitmap on the screen. self.bitmap = wx.StaticBitmap(self, bitmap=theBitmap)
Next, we will create an instance of the panel in a frame:
class MyFrame(wx.Frame): def __init__(self, parent, title=""): super(MyFrame, self).__init__(parent, title=title) # Set the panel self.panel = ImagePanel(self)
Run it and see the image shown on the panel:
The Bitmap
class is used to load image data from the usingBitmaps.png
file, which is located in the same directory as the script. This loads the PNG file data into a bitmap object, which can be used by the controls.
In this example, we used the StaticBitmap
control, which is one of the easiest ways to display a bitmap in the UI. This control takes the bitmap data object and handles drawing it on the screen.