Home About Me

Working with Checkbutton and Radiobutton in Tkinter

In Tkinter GUI programming, check buttons and radio buttons are among the most common ways to let users make choices. Tk’s built-in widgets for single and multiple selection are fairly minimal, but they are still practical enough for everyday desktop interfaces.

Checkbutton

A Checkbutton is used for multiple-choice style interaction. Since it represents a selectable state, the usual pattern is to detect whether the button is currently checked after it is clicked, and then perform different actions based on that state.

The key option here is variable. This attribute stores the current state of the check button, so the program can read it and react accordingly.

Tkinter also provides three useful methods for changing that state directly:

  • select() — mark it as selected
  • deselect() — clear the selection
  • toggle() — switch between selected and unselected

In the example below, clicking the check button changes the displayed text by reading the current value from the associated variable.

Radiobutton

A Radiobutton is usually shown as part of a group of two or more choices, where only one option can be active at a time. In practice, the most important thing is often the returned value.

A common approach is to prepare a list containing display text and the corresponding value, then generate the radio buttons with a for loop.

The variable=v and value=mode options work together. When get() is called on the shared variable, the result is the value assigned to the selected radio button. Because of that, it is important to set value deliberately when defining each option.

In this example, the selected value is a color string, and choosing a radio button changes the background color of the surrounding frame.

LabelFrame

LabelFrame is a container widget with a visible border and a title. It is useful when you want to group related controls together visually.

Screenshot

Example code

import tkinter as tk

def cdef():
    if var.get():
        strvar.set("看,我改变了!")
    else:
        strvar.set("有种你点我试试")

def rdoprt():
    print(v.get())
    r_frame['bg']=v.get()
root = tk.Tk()
root.title("Checkbutton和Radiobutton")

c_frame = tk.LabelFrame(root,text="Checkbutton", padx=5, pady=5)
c_frame.pack(fill=tk.X, side=tk.TOP)

var = tk.IntVar()
strvar = tk.StringVar()
strvar.set("有种你点我试试")
cbt = tk.Checkbutton(c_frame,textvariable=strvar, variable = var,command= cdef,)
# cbt.select()#选中
# print(var.get())
# cbt.deselect()#取消选中
# print(var.get())
# cbt.toggle()#切换选中开关
# print(var.get())
cbt.pack(side=tk.LEFT)
cbt1 = tk.Checkbutton(c_frame,text="Checkbutton", variable = var, command=cdef)
cbt1.pack(side=tk.LEFT)

r_frame = tk.LabelFrame(root,text="Radiobutton", padx=5, pady=5)
r_frame.pack(fill=tk.X, side=tk.TOP)
v = tk.StringVar()
v.set("L") # initialize
MODES = [
        ("#c00", "#c00"),
        ("#fff", "#fff"),
        ("#000", "#000"),
        ("#ccc", "#ccc"),
    ]

for text, mode in MODES:
        b = tk.Radiobutton(r_frame, text=text,variable=v, value=mode,command=rdoprt)
        b.pack(anchor=tk.W)

root.mainloop()

This small demo shows both widgets in a straightforward way: the check button updates its label text when toggled, while the radio buttons share one variable and use the selected value to change the frame background.