tk Checkbutton
Usage
- checkbutton_widget = tk.Checkbutton(parent, option=value)
- parent is the window or frame object.Options can be passed as parameters separated by commas.
Using check buttons
This code demonstrates the creation of check boxes (check buttons) and a text widget to display the selected options.
import tkinter as tk
from tkinter import font
def display_options():
selected_options = []
for i in range(len(options)):
if option_vars[i].get():
selected_options.append(options[i])
text_widget.delete(1.0, 'end')
text_widget.insert(tk.END, f"You selected: {', '.join(selected_options)}")
# Create the main window
root = tk.Tk()
root.title("Checkbutton Example")
# Create a frame with a background color
frame = tk.Frame(root, bg="light blue")
frame.pack(padx=10, pady=10, fill="both", expand=True)
# Define a font style
fontStyle = font.Font(family="Lucida Grande", size=18)
# Define the options
options = ["Option 1", "Option 2", "Option 3"]
# Create a list to hold the IntVar for each checkbutton
option_vars = []
for _ in options:
# default value is 0
option_vars.append(tk.IntVar())
# Create and pack the checkbuttons
for i in range(len(options)):
button = tk.Checkbutton(frame, text=options[i], variable=option_vars[i],
command=display_options, bg="white", fg="black", font=fontStyle, padx=10, pady=5)
button.pack(side="left", padx=5, pady=5)
# Create a text widget to display the selected options
text_widget = tk.Text(root, height=2, width=40, bg="white", fg="black",
font=fontStyle, bd=2, relief="solid")
text_widget.pack(padx=10, pady=10)
# Run the main event loop
root.mainloop()
For loops
These loops help create and manage the checkbuttons and their states, as well as display the selected options in the text widget.
Creating `IntVar` List:
option_vars = [] for _ in options: option_vars.append(tk.IntVar())
This loop iterates over the options list.
For each option, it creates a new IntVar instance and appends it to the option_vars list.
The underscore _ is used as a placeholder since the loop variable is not needed.
Creating and Packing Checkbuttons:
for i in range(len(options)): button = tk.Checkbutton(frame, text=options[i], variable=option_vars[i], command=display_options, bg="white", fg="black", font=fontStyle, padx=10, pady=5) button.pack(side="left", padx=5, pady=5)
This loop iterates over the indices of the options list using range(len(options)).
For each index i, it creates a Checkbutton with the corresponding text from options[i] and variable from option_vars[i].
The Checkbutton is then packed into the frame with specified padding and alignment.
Displaying Selected Options:
def display_options(): selected_options = [] for i in range(len(options)): if option_vars[i].get(): selected_options.append(options[i]) text_widget.delete(1.0, 'end') text_widget.insert(tk.END, f"You selected: {', '.join(selected_options)}")
This loop iterates over the indices of the options list.
For each index i, it checks if the corresponding IntVar in option_vars[i] is set (i.e., the checkbutton is selected).
If selected, the option from options[i] is appended to the selected_options list.
The text widget is then updated to display the selected options.
Parameter syntax
- checkbutton_widget = tk.Checkbutton(parent, option=value)
- parent is the window or frame object.Options can be passed as parameters separated by commas.
Parameters:
- activebackground
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, activebackground="color")Description: Sets the background color of the checkbutton when it is active.Default: SystemButtonFaceExample:checkbutton_widget = tk.Checkbutton(root, activebackground="lightblue")
- activeforeground
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, activeforeground="color")Description: Sets the foreground color of the checkbutton when it is active.Default: SystemWindowTextExample:checkbutton_widget = tk.Checkbutton(root, activeforeground="blue")
- anchor
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, anchor="position")Description: Sets the anchor position for the text and indicator.Default: centerExample:checkbutton_widget = tk.Checkbutton(root, anchor="w")
- background
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, background="color")Description: Sets the background color of the checkbutton.Default: SystemButtonFaceExample:checkbutton_widget = tk.Checkbutton(root, background="lightyellow")
- bd
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, bd=border_width)Description: Sets the border width of the checkbutton.Default: 2Example:checkbutton_widget = tk.Checkbutton(root, bd=5)
- bg
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, bg="color")Description: Sets the background color of the checkbutton.Default: SystemButtonFaceExample:checkbutton_widget = tk.Checkbutton(root, bg="lightyellow")
- bitmap
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, bitmap="bitmap_name")Description: Sets a bitmap image to be displayed on the checkbutton.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, bitmap="error")
- borderwidth
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, borderwidth=width)Description: Sets the width of the border around the checkbutton.Default: 2Example:checkbutton_widget = tk.Checkbutton(root, borderwidth=3)
- command
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, command=function)Description: Specifies a function to be called when the checkbutton is toggled.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, command=my_function)
- compound
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, compound="position")Description: Specifies how to display the image and text (if both are set).Default: noneExample:checkbutton_widget = tk.Checkbutton(root, compound="left")
- cursor
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, cursor="cursor_type")Description: Sets the mouse cursor when hovering over the checkbutton.Default: arrowExample:checkbutton_widget = tk.Checkbutton(root, cursor="hand2")
- disabledforeground
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, disabledforeground="color")Description: Sets the foreground color when the checkbutton is disabled.Default: SystemDisabledTextExample:checkbutton_widget = tk.Checkbutton(root, disabledforeground="gray")
- fg
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, fg="color")Description: Sets the foreground color of the checkbutton (text color).Default: SystemWindowTextExample:checkbutton_widget = tk.Checkbutton(root, fg="black")
- font
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, font=("font_name", size, "style"))Description: Specifies the font type, size, and style for the text of the checkbutton.Default: TkDefaultFontExample:checkbutton_widget = tk.Checkbutton(root, font=("Arial", 12, "bold"))
- height
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, height=value)Description: Sets the height of the checkbutton.Default: 0 (automatically determined)Example:checkbutton_widget = tk.Checkbutton(root, height=2)
- highlightbackground
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, highlightbackground="color")Description: Sets the background color of the checkbutton when it does not have focus.Default: SystemButtonFaceExample:checkbutton_widget = tk.Checkbutton(root, highlightbackground="gray")
- highlightcolor
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, highlightcolor="color")Description: Sets the color of the highlight when the checkbutton has focus.Default: SystemWindowFrameExample:checkbutton_widget = tk.Checkbutton(root, highlightcolor="blue")
- highlightthickness
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, highlightthickness=thickness)Description: Sets the thickness of the highlight border.Default: 1Example:checkbutton_widget = tk.Checkbutton(root, highlightthickness=2)
- image
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, image="image_name")Description: Sets an image to be displayed on the checkbutton.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, image=my_image)
- indicatoron
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, indicatoron=1)Description: Specifies whether to show the indicator (true or false).Default: 1Example:checkbutton_widget = tk.Checkbutton(root, indicatoron=0)
- justify
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, justify="position")Description: Sets the justification of the text (left, center, right).Default: centerExample:checkbutton_widget = tk.Checkbutton(root, justify="right")
- offrelief
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, offrelief="style")Description: Sets the relief style for the indicator when off.Default: raisedExample:checkbutton_widget = tk.Checkbutton(root, offrelief="flat")
- offvalue
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, offvalue=value)Description: Sets the value associated with the checkbutton when it is not checked.Default: 0Example:checkbutton_widget = tk.Checkbutton(root, offvalue=0)
- onvalue
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, onvalue=value)Description: Sets the value associated with the checkbutton when it is checked.Default: 1Example:checkbutton_widget = tk.Checkbutton(root, onvalue=1)
- overrelief
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, overrelief="style")Description: Sets the relief style for the indicator when hovered over.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, overrelief="sunken")
- padx
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, padx=padding_value)Description: Sets the horizontal padding within the checkbutton.Default: 1Example:checkbutton_widget = tk.Checkbutton(root, padx=10)
- pady
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, pady=padding_value)Description: Sets the vertical padding within the checkbutton.Default: 1Example:checkbutton_widget = tk.Checkbutton(root, pady=10)
- relief
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, relief="style")Description: Sets the border style of the checkbutton. Options include flat, raised, sunken, groove, ridge.Default: flatExample:checkbutton_widget = tk.Checkbutton(root, relief="raised")
- selectcolor
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, selectcolor="color")Description: Sets the color of the indicator when the checkbutton is selected.Default: SystemWindowExample:checkbutton_widget = tk.Checkbutton(root, selectcolor="lightgreen")
- selectimage
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, selectimage="image_name")Description: Sets an image to be displayed when the checkbutton is selected.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, selectimage=my_selected_image)
- state
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, state="state_type")Description: Sets the state of the checkbutton. Options include normal, disabled, or active.Default: normalExample:checkbutton_widget = tk.Checkbutton(root, state="disabled")
- takefocus
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, takefocus=1)Description: Allows the checkbutton to take focus on click.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, takefocus=1)
- text
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, text="label")Description: Sets the text label for the checkbutton.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, text="Option 1")
- textvariable
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, textvariable=variable)Description: Associates a variable with the text of the checkbutton.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, textvariable=my_text_var)
- tristateimage
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, tristateimage="image_name")Description: Sets an image to be displayed when the checkbutton is in a tri-state mode.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, tristateimage=my_tristate_image)
- tristatevalue
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, tristatevalue=value)Description: Sets the value associated with the checkbutton in a tri-state mode.Default: NoneExample:checkbutton_widget = tk.Checkbutton(root, tristatevalue=2)
- underline
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, underline=index)Description: Specifies the index of the character to underline in the text.Default: -1 (no underline)Example:checkbutton_widget = tk.Checkbutton(root, underline=0)
- variable
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, variable=control_variable)Description: Associates the checkbutton with a control variable (e.g., IntVar, StringVar).Default: !checkbutton-1Example:checkbutton_widget = tk.Checkbutton(root, variable=my_var)
- width
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, width=width_value)Description: Sets the width of the checkbutton.Default: 0 (automatically determined)Example:checkbutton_widget = tk.Checkbutton(root, width=30)
- wraplength
- Syntax:
checkbutton_widget = tk.Checkbutton(parent, wraplength=length)Description: Sets the line length for text wrapping in the checkbutton.Default: 0 (no wrapping)Example:checkbutton_widget = tk.Checkbutton(root, wraplength=100)