Book Image

wxPython Application Development Cookbook

By : Cody Precord
Book Image

wxPython Application Development Cookbook

By: Cody Precord

Overview of this book

wxPython is a GUI toolkit for the Python programming language built on top of the cross-platform wxWidgets GUI libraries. wxPython provides a powerful set of tools that allow you to quickly and efficiently building applications that can run on a variety of different platforms. Since wxWidgets provides a wrapper around each platform’s native GUI toolkit, the applications built with wxPython will have a native look and feel wherever they are deployed. This book will provide you with the skills to build highly functional and native looking user interfaces for Python applications on multiple operating system environments. By working through the recipes, you will gain insights into and exposure to creating applications using wxPython. With a wide range of topics covered in the book, there are recipes to get the most basic of beginners started in GUI programming as well as tips to help experienced users get more out of their applications. The recipes will take you from the most basic application constructs all the way through to the deployment of complete applications.
Table of Contents (17 chapters)
wxPython Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using bitmaps


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.

How to do it…

Perform the following steps:

  1. 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)
  2. 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)
  3. Run it and see the image shown on the panel:

How it works…

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.

There's more…

In this recipe, we used a PNG file as the source for the bitmap, but wx.Bitmap also supports a wide range of other image formats, such as BMP, GIF, ICO, ICON, JPEG, TIF, XBM, XPM, and several others, depending on the build of wxWidgets used by wxPython.