[Tkinter-discuss] Toggle buttons
Mark Summerfield
list at qtrac.plus.com
Fri Jul 13 09:35:20 CEST 2012
Hi Michael,
On Thu, 12 Jul 2012 19:13:36 +0200
Michael Lange <klappnase at web.de> wrote:
> Hi,
>
> Thus spoketh Mark Summerfield <list at qtrac.plus.com>
> unto us on Thu, 12 Jul 2012 11:38:37 +0100:
>
> > Hi,
> >
> > I want to create a toggle button, i.e., a button that when clicked goes
> > down (if it is up) and goes up (if it is down).
> >
> > One easy way to achieve this is to set the button's style to
> > "Toolbutton" (see self.toggle2). But unfortunately, that gets rid of
> > the button's relief so it looks out of place amongst other non-toggling
> > buttons.
> >
> > I solved if for Linux using a custom style (see self.toggle3). But this
> > doesn't work on Windows and I can't figure out how to solve it.
> >
> > Can anyone suggest a solution?
>
> Maybe you could use a Checkbutton with indicatoron=False, as in:
>
> import tkinter
>
> root = tkinter.Tk()
>
> def toggle(button, variable):
> if variable.get():
> button.config(text='On')
> else:
> button.config(text='Off')
>
> v1 = tkinter.BooleanVar()
> v1.set(False)
> b1 = tkinter.Checkbutton(root, text='Off', variable=v1,
> indicatoron=False, selectcolor='', command=lambda : toggle(b1, v1))
> b1.pack(padx=50, pady=50)
>
> root.mainloop()
>
> The button in this example looks tiny compared to a Button(), but this
> could probably easily be fixed. Unfortunately there does not seem to be a
> indicatoron Option for the ttk.Checkbutton though, and I am not sure how
> good or bad this will look on windows.
Unfortunately it looks bad on Windows because using tkinter.Checkbutton
rather than tkinter.ttk.Checkbutton means you get a button that isn't
styled so it looks completely different from the others.
I'll probably use "on" and "off" images on Windows.
Thanks!
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Advanced Qt Programming" - ISBN 0321635906
http://www.qtrac.eu/aqpbook.html
More information about the Tkinter-discuss
mailing list