wxPython Bitmap Button Widget

In this wxPython Tutorial, we will demonstrate how to use the Bitmap Button Widget, alongside it’s various styles, features and functions. A complete list of options will be included here, alongside several code examples for your convenience.


wxPython Bitmap Button Syntax

The syntax for creating a Bitmap Button Widget.

bitmap = BitmapButton(parent, id, bitmap, pos, size, style, validator, name)
  1. parent: This is the Widget to which it is parented, such as a Panel.
  2. id: Widget ID. Default value is ID_ANY, which gives it the next available ID.
  3. bitmap: The Bitmap which you want to display on the Widget.
  4. pos: A tuple containing the coordinates of where the topleft corner of the Bitmap Widget should begin from.
  5. size: A tuple which defines the dimensions of the area occupied by the Widget.
  6. style: Used for styling the Button (such as Alignment)

wxPython Bitmap Button Methods

As a Subclass of the Button Widget, the Bitmap Button can use it’s methods as well. Head over to the Button Widget page to check them out.


wxPython Bitmap Button Styles

A list of available styles for the Bitmap Button Widgets.

StyleDescription
wx.BU_LEFTAligns the Bitmap label to the left
wx.BU_TOPAligns the Bitmap label to the top
wx.BU_RIGHTAligns the Bitmap label to the right
wx.BU_BOTTOMAligns the Bitmap label to the bottom

Example Code

In this example we will create two Bitmap Buttons and assign two Bitmap images to them.

Using wx.Bitmap() we can load a Bitmap image into our code, ready for use. We can either use the setBitmap(bitmap) method, or just directly pass the created Bitmap into the constructor for the Bitmap Buttons.

import wx

class Window(wx.Frame):
    def __init__(self, title):
        super().__init__(parent = None, title = title)
        self.panel = wx.Panel(self)

        openBitmap = wx.Bitmap("Open.bmp")
        saveBitmap = wx.Bitmap("Save.bmp")

        button1 = wx.BitmapButton(self.panel, bitmap = openBitmap, pos = (50, 50))
        button2 = wx.BitmapButton(self.panel, bitmap = saveBitmap, pos = (150, 50))

        self.Centre()
        self.Show()
        
app = wx.App()
window = Window("WxPython Tutorial")
app.MainLoop()

The output of the above code:

wxPython Bitmap Button

Reminder! Bitmap buttons have been somewhat replaced by the Button widget which now includes functionality for adding Bitmap images as well. It has the added benefit of displaying both text and an Image alongside each other.


This marks the end of the wxPython Bitmap Button Widget. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments