A simple way to create a toggle button for the Tkinter Python GUI toolkit.
Simple Tkinter Toggle Button
# use config() to create a Tkinter toggle button
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
def toggle():
'''
use
t_btn.config('text')[-1]
to get the present state of the toggle button
'''
if t_btn.config('text')[-1] == 'True':
t_btn.config(text='False')
else:
t_btn.config(text='True')
root = tk.Tk()
t_btn = tk.Button(text="True", width=12, command=toggle)
t_btn.pack(pady=5)
root.mainloop()
woooee 814 Nearly a Posting Maven
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague
Lardmeister 461 Posting Virtuoso
BustACode 15 Light Poster
BustACode 15 Light Poster
martineau 19 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
rproffitt commented: “SHOW ME WHAT YOU GOT! I WANT TO SEE WHAT YOU GOT!" - Cromulons. +15
martineau 19 Newbie Poster
martineau 19 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
martineau 19 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.