Quantcast

cross-platform Wx.Button with icon and label

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

cross-platform Wx.Button with icon and label

John Lane

I would like to get consistent buttons across Linux, MacOS, and Windows.

I use this generic code to add buttons with icons and/or labels:

def superbutton(id=wx.ID_ANY, parent=None, style=0, sizer=None,
label=None, icon=None, tooltip=None, callback=None, data=None,
icon_only=False):
    """
    Add a button
    The icon may be one of:
    * GTK+ icon
(https://www.wxpython.org/docs/api/wx.ArtProvider-class.html)
    * a file path such as 'icon.png' or '/path/to/icon.png'
    * a wx.Bitmap such as `wx.Bitmap('icon.png')`
    """
    if not parent: parent = self
    button = wx.Button(parent, id, style=style)
    if label and not icon_only: button.SetLabel(label) #####
    if icon:
      if isinstance(icon, basestring):
        if icon[:5] == 'wxART':
          icon = wx.ArtProvider.GetBitmap(icon, wx.ART_MENU, size=(16,16))
        else:
          icon = wx.Bitmap(icon)
      if type(icon) is wx.Bitmap:
        button.SetBitmapLabel(icon)
    if tooltip:  button.SetToolTipString(tooltip)
    if callback:
      button.Bind(wx.EVT_BUTTON,
        (lambda evt, data=data: callback(evt, data=data)) if data else
callback)
    if sizer:    sizer.Add(button)
    return button

I use it like this example:

superbutton(parent=self.content, sizer=sizer, id=wx.ID_ANY,
callback=self.go, label='Go',
icon=os.path.join(os.path.dirname(__file__), '..', 'icons', 'go.png'))

It works as expected on GTK/Linux and Windows but doesn't give the
expected result on MacOS.

The expectation is that a button can be created with a label AND an icon
such that the button has an icon on the left followed by a label. But on
MacOS the above code results in a button without a label.

If I move the SetLabel line (marked with #####) to after the label set
(before the tooltip set) then I do get both an icon and label but they
are centred and appear one above the other. Furthermore, this change
stops the label appearing on Linux.

What am I doing wrong - how can I achieve consistent buttons across the
three platforms?

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cross-platform Wx.Button with icon and label

Vadim Zeitlin-4
On Thu, 18 May 2017 11:08:16 +0100 John Lane wrote:

JL> I would like to get consistent buttons across Linux, MacOS, and Windows.
...
JL>     button = wx.Button(parent, id, style=style)
JL>     if label and not icon_only: button.SetLabel(label) #####
JL>     if icon:
JL>       if isinstance(icon, basestring):
JL>         if icon[:5] == 'wxART':
JL>           icon = wx.ArtProvider.GetBitmap(icon, wx.ART_MENU, size=(16,16))
JL>         else:
JL>           icon = wx.Bitmap(icon)
JL>       if type(icon) is wx.Bitmap:
JL>         button.SetBitmapLabel(icon)
...
JL> It works as expected on GTK/Linux and Windows but doesn't give the
JL> expected result on MacOS.

 Which version of wxWidgets do you use? I seem to remember a bug in the
related code, but I think it must have been fixed in 3.1.0.

JL> What am I doing wrong - how can I achieve consistent buttons across the
JL> three platforms?

 Using SetLabel() and SetBitmapLabel() is supposed to work, but, of course,
bugs are always possible, especially because this code is a bit complicated
as different controls need to be created internally depending on which one
of those is [not] used...

 Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
               http://www.tt-solutions.com/

attachment0 (203 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: cross-platform Wx.Button with icon and label

John Lane
On 18/05/17 20:59, Vadim Zeitlin wrote:

>  Which version of wxWidgets do you use? I seem to remember a bug in the
> related code, but I think it must have been fixed in 3.1.0.

I am running wxPython as follows

* Linux: 3.0.2.0 gtk2 (classic)

* Windows: 3.0.2.0 msw (classic)

* Mac: 3.0.3.0.b20160914 osx-cocoa (classic)

These are the versions available at https://wxpython.org/download.php
except for the mac version (as per http://stackoverflow.com/a/40988207)
which came from here:

*
https://wxpython.org/snapshots/wxPython3.0-osx-3.0.3.0.b20160914-cocoa-py2.7.dmg
.

However, I have fixed it!

I added `button.SetBitmapPosition(wx.LEFT)` after calling
`SetBitmapLabel` and that one change allowed the original code (without
needing to move the SetLabel() call) to work on all three platforms:

    if icon:
      if isinstance(icon, basestring):
        if icon[:5] == 'wxART':
          icon = wx.ArtProvider.GetBitmap(icon, wx.ART_MENU, size=(16,16))
        else:
          icon = wx.Bitmap(icon)
      if type(icon) is wx.Bitmap:
        button.SetBitmapLabel(icon)
        button.SetBitmapPosition(wx.LEFT)


--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to [hidden email]
or visit http://groups.google.com/group/wx-users
Loading...