Tkinter Checkbutton – Selected / Checked by default
We can make the Checkbutton selected/checked by default when displayed for the first time in GUI by using the variable associated with the Checkbutton.
To create a Checkbutton widget that is selected/checked by default in Tkinter, set the value of the variable associated with the Checkbutton to 1.
For example, consider the following code snippet.
checkbutton_var = tk.IntVar()
checkbutton_1 = tk.Checkbutton(window, text="Option 1", variable=checkbutton_var)
checkbutton_var.set(1)
checkbutton_var is used for the variable option to create the Checkbutton checkbutton_1. To make this checkbutton selected by default, we have set the value of the checkbutton_var to 1 using set() method.
In this tutorial, you will learn how to make a Checkbutton checked by default, with examples.
Example
In the following GUI application, we shall create a Checkbutton widget that is selected by default.
Python Program
import tkinter as tk
# Create the main window
window = tk.Tk()
window.geometry("300x200")
window.title("PythonExamples.org")
# Create a variable to store the checkbutton state
checkbutton_var = tk.IntVar()
# Create the checkbutton widget
checkbutton_1 = tk.Checkbutton(window, text="Option 1", variable=checkbutton_var)
# Set the initial checkbutton state
checkbutton_var.set(1)
# Pack the checkbutton widget
checkbutton_1.pack()
# Start the Tkinter event loop
window.mainloop()
CopyOutput in MacOS
Summary
In this Python Tkinter tutorial, we have seen how to create a Checkbutton widget that is selected or checked by default, with examples.