[Tkinter-discuss] Toggle buttons

Michael Lange klappnase at web.de
Thu Jul 12 19:13:36 CEST 2012


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.

Regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

You!  What PLANET is this!
		-- McCoy, "The City on the Edge of Forever", stardate
                   3134.0


More information about the Tkinter-discuss mailing list