Looking for an "on/off" toggle control

160 views
Skip to first unread message

mbre...@gmail.com

unread,
Jan 12, 2021, 8:38:57 AM1/12/21
to wxPython-users
Dear All

I am looking for a wxPython control that looks like an "on/off" switch. Something like this:

I'd prefer a native implementation. I couldn't find anything in the docs, but maybe I am not looking at the right place. Any pointers?

Thanks
Matthias
Message has been deleted

ioprst

unread,
Jan 12, 2021, 11:26:28 AM1/12/21
to wxPython-users
You can use wx.ToggleButton and change the button text using the SetLabel method.

Charles McKnight

unread,
Jan 12, 2021, 12:01:03 PM1/12/21
to wxpytho...@googlegroups.com
Check this example for the  wxToggleButton:  http://zetcode.com/wxpython/widgets/

Just place the buttons side by side and change the appearance/text when the state of one changes.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/7fa13b5b-299c-4ee5-8f95-a3036806a107n%40googlegroups.com.

Rolf

unread,
Jan 12, 2021, 1:27:57 PM1/12/21
to wxpytho...@googlegroups.com
Simple demo of wx.ToggleButton


import wx

class Example(wx.Frame):
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)
        pnl = wx.Panel(self)
        self.On = wx.ToggleButton(pnl, label='On', size=(40, -1),
pos=(20, 25))
        self.Off = wx.ToggleButton(pnl, label='', size=(40, -1),
pos=(60, 25))
        self.On.SetValue(True)

        self.On.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleOn)
        self.Off.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleOff)

        self.SetSize((250, 150))
        self.SetTitle('On/Off Toggle button')
        self.Show()

    def ToggleOn(self, event):
        self.On.SetLabel("On")
        self.On.SetValue(True)
        self.Off.SetValue(False)
        self.Off.SetLabel("")

    def ToggleOff(self, event):
        self.Off.SetLabel("Off")
        self.Off.SetValue(True)
        self.On.SetValue(False)
        self.On.SetLabel("")

if __name__ == "__main__":
    app = wx.App()
    ex = Example(None)
    app.MainLoop()
> --
> You received this message because you are subscribed to the Google
> Groups "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to wxpython-user...@googlegroups.com
> <mailto:wxpython-user...@googlegroups.com>.
> <https://groups.google.com/d/msgid/wxpython-users/7fa13b5b-299c-4ee5-8f95-a3036806a107n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Matthias Brennwald

unread,
Jan 12, 2021, 4:57:40 PM1/12/21
to wxpytho...@googlegroups.com
The functionality of wx.ToggleButton and wx.CheckBox is very close to what I am looking for, but the look and feel is rather different to the "Switch" I am looking for:

Is there a native "wx.Switch" thingy?



On Tue, Jan 12, 2021 at 08:21, ioprst <ioprs...@gmail.com> wrote:
wx.CheckBox or wx.ToggleButton?

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/e660b084-c225-4488-9fc1-7b6eec55431bn%40googlegroups.com.

john fabiani

unread,
Jan 12, 2021, 5:55:28 PM1/12/21
to wxpytho...@googlegroups.com

Matthias Brennwald

unread,
Jan 13, 2021, 11:07:05 AM1/13/21
to john fabiani, wxpython-users


On Di, Jan 12, 2021 at 14:52, john fabiani <fabian...@gmail.com> wrote:

I think you are stuck with the toggle switch.  But if you decide to draw one using a custom wxPython Widget let us know.

I believe it would be awkward to re-invent the wheel since the main GUI systems out there already do have their native implementations of an "on/off" switch, which were designed with a look-and-feel that goes well with the respective GUI system. Would be it be possible to make use of these native controls in wxPython? (I have never done anything on this level, so I am pretty clueless about how this might work).

Scott Talbert

unread,
Jan 13, 2021, 11:21:40 AM1/13/21
to wxpython-users, john fabiani
On Wed, 13 Jan 2021, Matthias Brennwald wrote:

> I think you are stuck with the toggle switch.  But if you decide
> to draw one using a custom wxPython Widget let us know.
>
> I believe it would be awkward to re-invent the wheel since the main GUI
> systems out there already do have their native implementations of an
> "on/off" switch, which were designed with a look-and-feel that goes well
> with the respective GUI system. Would be it be possible to make use of these
> native controls in wxPython? (I have never done anything on this level, so I
> am pretty clueless about how this might work).

You would probably need to get the support added in wxWidgets first, and
then wxPython could wrap it. You could file a wxWidgets ticket at
https://trac.wxwidgets.org/

Scott

Tim Roberts

unread,
Jan 13, 2021, 2:41:43 PM1/13/21
to wxpytho...@googlegroups.com
Matthias Brennwald wrote:
>
> I believe it would be awkward to re-invent the wheel since the main
> GUI systems out there already do have their native implementations of
> an "on/off" switch, which were designed with a look-and-feel that goes
> well with the respective GUI system.

Windows, for one, does not.  That niche is filled by the checkbox.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.


john fabiani

unread,
Jan 13, 2021, 3:01:51 PM1/13/21
to wxpytho...@googlegroups.com
I normally don't use anything from windows 10 uwp - but I am fairly sure
the Toggle switch is available.

Also "re-inventing"  - if you are not willing to write a custom widget
then you will either have to wait until some future version that has the
control built-in or make do with the checkbox.  You must be aware that
for many many years the checkbox was all that was available on all
platforms.  It seem to work just fine.  The switch is a new UI
development as are many others.  I happen to like it - but many others I
don't believe add anything other than change for changes sake.  That
said, I believe the current toggle switch conveys the action of being
"on or off" very well.

Johnf

Matt Newville

unread,
Jan 13, 2021, 3:46:26 PM1/13/21
to wxpytho...@googlegroups.com
On Wed, Jan 13, 2021 at 2:01 PM john fabiani <fabian...@gmail.com> wrote:
I normally don't use anything from windows 10 uwp - but I am fairly sure
the Toggle switch is available.

Also "re-inventing"  - if you are not willing to write a custom widget
then you will either have to wait until some future version that has the
control built-in or make do with the checkbox.  You must be aware that
for many many years the checkbox was all that was available on all
platforms.  It seem to work just fine.  The switch is a new UI
development as are many others.  I happen to like it - but many others I
don't believe add anything other than change for changes sake.  That
said, I believe the current toggle switch conveys the action of being
"on or off" very well.

References: 

Both companies suggest that toggle switches have a different purpose from a checkbox, implying both immediate action (as opposed to the selection of a preference) and propagation to other GUI elements, turning on/off a whole category of options and displays for example.

--Matt

john fabiani

unread,
Jan 13, 2021, 5:18:04 PM1/13/21
to wxpytho...@googlegroups.com

I agree with your links.  That said - all we had was a checkbox or the toggle switch and some how we made due. 

Johnf

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.

Steve Barnes

unread,
Jan 13, 2021, 11:43:17 PM1/13/21
to wxpytho...@googlegroups.com

Of course the agw SBitmapToggleButton could be used to provide a non-native switch effect. It would still be a button rather than a switch but with the appropriate bitmaps could give the appearance of just about any switch.

Matthias Brennwald

unread,
Jan 14, 2021, 1:39:48 AM1/14/21
to wxpytho...@googlegroups.com
Wouldn't bitmapped buttons ask for trouble with todays variety of screen resolutions?

Steve Barnes

unread,
Jan 14, 2021, 3:04:20 AM1/14/21
to wxpytho...@googlegroups.com

It adds some complexity as you need to either provide and select from multiple bitmaps or scale the bitmaps (preferably down) both based on the current screen resolution but it is available now.

Rolf

unread,
Jan 21, 2021, 11:13:51 AM1/21/21
to wxpytho...@googlegroups.com
Whilst I agreed with Johnf, I decided that I liked the idea of an on/off
button, so while this is not of industrial strength, it serves the
purpose for my requirements.

You may want to alter the code to fit yours.

I enclose the code, the demo image and an image of the control in one of
my projects.

Use at your own discretion.

Usage:

'python onoffbutton.py'

and

'import onoffbutton as oob' then 'self.fc_logging =
oob.OnOffButton(self.tab7, wx.ID_ANY)' as a drop-in replacement for CheckBox

Regards

Rolf

demo project
On 14/01/2021 07:39, Matthias Brennwald wrote:
> Wouldn't bitmapped buttons ask for trouble with todays variety of
> screen resolutions?
>
> On Do, Jan 14, 2021 at 04:43, Steve Barnes <Gadge...@live.co.uk> wrote:
>>
>> Of course the agw SBitmapToggleButton could be used to provide a
>> non-native switch effect. It would still be a button rather than a
>> switch but with the appropriate bitmaps could give the appearance of
>> just about any switch.
>>
>>  
>>
>> *From:*wxpytho...@googlegroups.com
>> <wxpytho...@googlegroups.com> *On Behalf Of *john fabiani
>> *Sent:* 13 January 2021 22:18
>> *To:* wxpytho...@googlegroups.com
>> *Subject:* Re: [wxPython-users] Re: Looking for an "on/off" toggle
>> <https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fuwp%2Fdesign%2Fcontrols-and-patterns%2Ftoggles&data=04%7C01%7C%7Ca2a95f59c0cb4a03ee8608d8b81119cb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637461730867743993%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=tpJyuSTDHH9cXv61MujvETweguvJba740XGFmw%2BhWpE%3D&reserved=0>
>>
>>  
>>  https://developer.apple.com/design/human-interface-guidelines/macos/buttons/switches/
>> <https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdeveloper.apple.com%2Fdesign%2Fhuman-interface-guidelines%2Fmacos%2Fbuttons%2Fswitches%2F&data=04%7C01%7C%7Ca2a95f59c0cb4a03ee8608d8b81119cb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637461730867773977%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=tu8c3inyJ9zsPEfg%2BQdZII0Wjjj7Fp1YYTBNEeAr2f0%3D&reserved=0>
>>
>>  
>>
>> Both companies suggest that toggle switches have a different
>> purpose from a checkbox, implying both immediate action (as
>> opposed to the selection of a preference) and propagation to
>> other GUI elements, turning on/off a whole category of options
>> and displays for example.
>>
>>  
>>
>> --Matt
>>
>>  
>>
>> --
>> You received this message because you are subscribed to the
>> Google Groups "wxPython-users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to wxpython-user...@googlegroups.com
>> <mailto:wxpython-user...@googlegroups.com>.
>> <https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fwxpython-users%2FCA%252B7ESbqwVdq%253DyXbNypgGNHCWnuM4Qe_0T1i7Tuk3yNjnQspzbg%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=04%7C01%7C%7Ca2a95f59c0cb4a03ee8608d8b81119cb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637461730867783972%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=QkXnY58ZGooOPnVaWxbKZgLbpJLAOsxBVEMxEyaT6J0%3D&reserved=0>.
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "wxPython-users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to wxpython-user...@googlegroups.com
>> <mailto:wxpython-user...@googlegroups.com>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/wxpython-users/48ec4c47-d518-9975-3391-5f6e5f2d29cc%40gmail.com
>> <https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fwxpython-users%2F48ec4c47-d518-9975-3391-5f6e5f2d29cc%2540gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=04%7C01%7C%7Ca2a95f59c0cb4a03ee8608d8b81119cb%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637461730867793966%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=vCIvhd%2B24K2P4h4R3Dd%2Fp9NhPp7Mf1NXrFkzEZISRoo%3D&reserved=0>.
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "wxPython-users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to wxpython-user...@googlegroups.com
>> <mailto:wxpython-user...@googlegroups.com>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/wxpython-users/VI1PR03MB44790BABF461FD2FEF12BE8D9BA80%40VI1PR03MB4479.eurprd03.prod.outlook.com
>> <https://groups.google.com/d/msgid/wxpython-users/VI1PR03MB44790BABF461FD2FEF12BE8D9BA80%40VI1PR03MB4479.eurprd03.prod.outlook.com?utm_medium=email&utm_source=footer>.
> --
> You received this message because you are subscribed to the Google
> Groups "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to wxpython-user...@googlegroups.com
> <mailto:wxpython-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/wxpython-users/0UVWMQ.7I5FXIYNQBZJ1%40gmail.com
> <https://groups.google.com/d/msgid/wxpython-users/0UVWMQ.7I5FXIYNQBZJ1%40gmail.com?utm_medium=email&utm_source=footer>.
onoffbutton.py

john fabiani

unread,
Jan 21, 2021, 2:19:03 PM1/21/21
to wxpytho...@googlegroups.com
Thank you so much.  I have downloaded it and will use it in the future
for sure!

Johnf

Rolf

unread,
Jan 21, 2021, 2:41:33 PM1/21/21
to wxpytho...@googlegroups.com
I found a use case ( str(xxx.GetValue()) where it fails logically where
I expected "True" or "False", that may just be my code.

So change the GetValue function in onoffbutton.py to:

    def GetValue(self):
        if self._Value:
            return True
        else:
            return False


As always, Beware there be dragons!

Regards,

Rolf
> an email to wxpython-user...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/wxpython-users/ac2e7eb8-12a5-3acb-e973-6f21d7660a0d%40gmail.com.

john fabiani

unread,
Jan 21, 2021, 6:21:17 PM1/21/21
to wxpytho...@googlegroups.com
There are two vars that appear not to be used - "imgh" and "imgw"  can
they be removed?

johnf

 def SetImageSize(self,img):
        imgsize = img.GetSize()
        imgh = imgsize[1]
        imgw = imgsize[0]
        w = self._size[0]
        h = self._size[1]

Rolf

unread,
Jan 22, 2021, 1:36:34 AM1/22/21
to wxpytho...@googlegroups.com
Yes!

They are hangovers from trying to size the image without resorting to
image scaling.

It seems that imgsize is superfluous as well as "imgh" and "imgw".

As you can see, if someone knows their way around DC's (which I don't)
there's plenty of room for improvement.

In the end, I went for "that will do".

Rolf
> https://groups.google.com/d/msgid/wxpython-users/1d653b69-06f1-221f-7158-e8696c2eafa4%40gmail.com.

john fabiani

unread,
Jan 22, 2021, 12:45:03 PM1/22/21
to wxpytho...@googlegroups.com
As any true programmer would do - it works don't mess with it! LOL!

Johnf

Rolf

unread,
Jan 23, 2021, 6:23:25 AM1/23/21
to wxpytho...@googlegroups.com
Damn, you should have mentioned that before I messed with it again.

This version sorts out the images component ratios, allowing the image
to be sized in the DC, thus removing the need to Scale it.

I had hoped that resolving this, would allow me to change the font size
on the label, alas, it's still proving troublesome.

The font changes alright, it just doesn't resize the statictext component,

Still, as I never use the label on a CheckBox, the rule "It works for
me", still applies.

Regards,

Rolf

p.s. If you spot my font omission or any bugs drop me a line.
> https://groups.google.com/d/msgid/wxpython-users/fceb3b36-7243-10f8-6f43-8fa16f80c64e%40gmail.com.
onoffbutton.py

Rolf

unread,
Jan 25, 2021, 11:23:11 AM1/25/21
to wxpytho...@googlegroups.com
Hopefully, this is the final version of OnOffbutton, short of bug fixes.

The discovery of dc.SetPen() cleaned up the image.

For Font size changes, I believe I've resolved the Label and Control
sizing for both manual and sizer layouts, including multiline labels
(within reason).

Plus partial sizes e.g. (40, -1) should be catered for.

Submitted in the hope that someone finds it useful.

Rolf

demo image
On 22/01/2021 18:44, john fabiani wrote:
> https://groups.google.com/d/msgid/wxpython-users/fceb3b36-7243-10f8-6f43-8fa16f80c64e%40gmail.com.
onoffbutton.py

Rolf

unread,
Jan 29, 2021, 6:31:27 AM1/29/21
to wxpytho...@googlegroups.com
I apologise for yet another post on this subject but I didn't notice
that wx.CheckBox has an ALIGN_RIGHT style.

That being the case, I've added the ability to Align the control to the
right of the Label.

In doing so, I've also had to address "spacing" between the control and
the Label.

Given the need to make changes to address the omission, I've taken the
opportunity to add a very simple rectangular option.

So the control has the default Rounded rectangle look and a very simple
Rectangle look.

Once again, I hope this is the last version.

Demo image


On 22/01/2021 18:44, john fabiani wrote:
> https://groups.google.com/d/msgid/wxpython-users/fceb3b36-7243-10f8-6f43-8fa16f80c64e%40gmail.com.
onoffbutton.py

john fabiani

unread,
Jan 29, 2021, 1:30:50 PM1/29/21
to wxpytho...@googlegroups.com
Thanks

Johnf

Rolf

unread,
Feb 2, 2021, 10:55:08 AM2/2/21
to wxpytho...@googlegroups.com
Lord help me but it occurred that a simplistic bitmap toggling option,
might be all some require.

A widget that you give 2 images and On Click it swaps them over.

It's very simple and not fully fledged but in certain circumstances may
be useful.

It essentially takes the wx.BitmapToggleButton to it's logical
conclusion i.e. it actually toggles bitmaps rather than flipping a
bitmap between an enabled and disabled look.

The "Disabled" bitmap is simply a greyed out version of the On Bitmap in
is simple version.

To run the demonstration unzip it and run "python ToggleButtonBitmap.py"
from the command line.

Regards Rolf


Demo image
> https://groups.google.com/d/msgid/wxpython-users/38c2077e-9885-8482-7462-387d22e515ae%40gmail.com.
ToggleButtonBitmap.tar.gz
Reply all
Reply to author
Forward
0 new messages