Thursday, October 23, 2025
HomeLanguageswxPython – Image on button in Python

wxPython – Image on button in Python

In this particular article we will learn how can we add image to a button in GUI using wxPython. This can be achieved using BitmapButton() constructor of wx.BitmapButton class in wx. Following Window Styles are supported :

  • wx.BU_LEFT: Left-justifies the bitmap label.
  • wx.BU_TOP: Aligns the bitmap label to the top of the button.
  • wx.BU_RIGHT: Right-justifies the bitmap label.
  • wx.BU_BOTTOM: Aligns the bitmap label to the bottom of the button.

Syntax :

wx.StaticText(self, parent, id=ID_ANY, bitmap=NullBitmap, 
              pos=DefaultPosition, size=DefaultSize, style=0, 
           validator= DefaultValidator, name=StaticTextNameStr)

Parameters :

Parameter Input Type Description
parent wx.Window Parent window. Should not be None.
id wx.WindowID Control identifier. A value of -1 denotes a default value.
bitmap wx.Bitmap Bit to be displayed.
pos wx.Point Window position.
size wx.Window Window size.
style long Window style.
validator wx.Validator Window validator.
name string Window name.

Example Code: 

Python3




# import wxPython
import wx
 
# event function for button
def onButton(event):
    print("Button pressed.")
 
app = wx.App()
frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0, 0, 200, 70)
panel = wx.Panel(frame, wx.ID_ANY)
 
# open image from disk
bmp = wx.Bitmap("/home/rahul101/Desktop/wxPython/images.png", wx.BITMAP_TYPE_ANY)
# create image button using BitMapButton constructor
button = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bmp,
                          size =(bmp.GetWidth()+10, bmp.GetHeight()+10))
  
button.Bind(wx.EVT_BUTTON, onButton)
button.SetPosition((10, 10))
 
frame.Show()
frame.Centre()
app.MainLoop()


Output :

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS