Skip to content
Related Articles

Related Articles

wxPython – Image on button in Python
  • Last Updated : 10 May, 2020
GeeksforGeeks - Summer Carnival Banner

In this particular article we will learn how can we add image to a button in GUI using wxPython. This can be achievwd 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= DefaultVadator, name=StaticTextNameStr)

Parameters :

ParameterInput TypeDescription
parentwx.WindowParent window. Should not be None.
idwx.WindowIDControl identifier. A value of -1 denotes a default value.
bitmapwx.BitmapBitto be displayed.
poswx.PointWindow position.
sizewx.WindowWindow size.
stylelongWindow style.
validatorwx.ValidatorWindow validator.
namestringWindow name.

Example Code:






# 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 :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :