Search code examples
python-3.xwxpython

Adding an image to a button


I have this:

GUI

Here is some code that created the above image:

hbox = wx.BoxSizer(wx.HORIZONTAL)
_img = wx.StaticBitmap()
_img.Create(parent, label=wx.Bitmap(os.path.join(
    os.path.dirname(__file__), 'images', 'exit-to-app.svg')))
hbox.Add(_img, proportion=1, flag=wx.ALIGN_CENTER | wx.ALL)
_exit = wx.Button(parent, label="Exit")
_exit.SetBackgroundColour('#5968c3')
self.Bind(wx.EVT_BUTTON, self.OnQuit, _exit)
hbox.Add(_exit, proportion=1, flag=wx.ALIGN_CENTER | wx.ALL)
return hbox

How to add the bitmap to the button?


Solution

  • wx.Button has supported adding a bitmap to the label for a while now, although on GTK builds I think it also depends on a global preference whether the icon will be displayed or not, and probably also on the active theme. See the Button sample in the demo for an example.

    https://github.com/wxWidgets/Phoenix/blob/master/demo/Button.py#L28