Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Checkbutton in Tkinter

The Checkbutton widget in tkinter is used to display a number of options to a user as toggle buttons. The user can then select one or more options by ticking the checkboxes.

Let's create a tutorial on how to use the Checkbutton widget in tkinter:

Checkbutton in tkinter Tutorial:

1. Import Required Libraries:

import tkinter as tk

2. Basic Checkbutton Creation:

To create a basic checkbutton, simply use:

check_btn = tk.Checkbutton(root, text="Option 1")
check_btn.pack()

3. Using Variables with Checkbuttons:

To track the state of a checkbutton (whether it's checked or not), you typically use a tk.IntVar():

check_var = tk.IntVar()
check_btn = tk.Checkbutton(root, text="Option 1", variable=check_var)
check_btn.pack()

With the above setup, check_var.get() will return 1 if the checkbox is checked and 0 otherwise.

4. Using Commands with Checkbuttons:

You can also execute a function when a checkbutton is toggled:

def on_toggle():
    if check_var.get():
        print("Checked!")
    else:
        print("Unchecked!")

check_btn = tk.Checkbutton(root, text="Option 1", variable=check_var, command=on_toggle)
check_btn.pack()

5. Complete Code Example:

Let's create a GUI where there are multiple checkbuttons and a button to show which options are selected:

import tkinter as tk

def display_selected_options():
    selected = []
    if option1_var.get():
        selected.append("Option 1")
    if option2_var.get():
        selected.append("Option 2")
    if option3_var.get():
        selected.append("Option 3")
    
    result_label.config(text="Selected: " + ", ".join(selected))

root = tk.Tk()
root.title("Checkbutton Tutorial")

option1_var = tk.IntVar()
option2_var = tk.IntVar()
option3_var = tk.IntVar()

check_btn1 = tk.Checkbutton(root, text="Option 1", variable=option1_var)
check_btn1.pack(anchor=tk.W, padx=20, pady=5)

check_btn2 = tk.Checkbutton(root, text="Option 2", variable=option2_var)
check_btn2.pack(anchor=tk.W, padx=20, pady=5)

check_btn3 = tk.Checkbutton(root, text="Option 3", variable=option3_var)
check_btn3.pack(anchor=tk.W, padx=20, pady=5)

show_btn = tk.Button(root, text="Show Selected Options", command=display_selected_options)
show_btn.pack(pady=20)

result_label = tk.Label(root, text="Selected: None")
result_label.pack(pady=20)

root.mainloop()

In this example, there are three checkbuttons. Clicking the "Show Selected Options" button will display which options are currently checked. This information is updated in a label below the button.

  1. Python Tkinter create Checkbutton example:

    • Description: Checkbuttons in Tkinter are used to represent a binary choice. They can be either selected (checked) or unselected (unchecked).
    • Code:
      import tkinter as tk
      
      def on_checkbox_click():
          print("Checkbutton clicked")
      
      root = tk.Tk()
      
      checkbox = tk.Checkbutton(root, text="Enable Feature", command=on_checkbox_click)
      checkbox.pack()
      
      root.mainloop()
      
  2. How to use Checkbutton widget in Tkinter:

    • Description: The Checkbutton widget in Tkinter is used to create checkboxes. It can be associated with a variable to track its state.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Variable to track the state of the checkbox
      var = tk.IntVar()
      
      checkbox = tk.Checkbutton(root, text="Enable Feature", variable=var)
      checkbox.pack()
      
      root.mainloop()
      
  3. Creating checkboxes with Checkbutton in Tkinter:

    • Description: Multiple checkboxes can be created using the Checkbutton widget. Each checkbox can be associated with a different variable.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      var1 = tk.IntVar()
      var2 = tk.IntVar()
      
      checkbox1 = tk.Checkbutton(root, text="Option 1", variable=var1)
      checkbox2 = tk.Checkbutton(root, text="Option 2", variable=var2)
      
      checkbox1.pack()
      checkbox2.pack()
      
      root.mainloop()
      
  4. Python Tkinter Checkbutton variable:

    • Description: The variable associated with a Checkbutton holds the state of the checkbox (0 for unchecked, 1 for checked).
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      var = tk.IntVar()
      
      checkbox = tk.Checkbutton(root, text="Enable Feature", variable=var)
      checkbox.pack()
      
      # Accessing the state of the checkbox
      state = var.get()
      print("Checkbox state:", state)
      
      root.mainloop()
      
  5. Checkbutton widget events and callbacks in Tkinter:

    • Description: Checkbuttons can trigger events and callbacks when their state changes. This is achieved using the command option.
    • Code:
      import tkinter as tk
      
      def on_checkbox_click():
          print("Checkbutton clicked")
      
      root = tk.Tk()
      
      checkbox = tk.Checkbutton(root, text="Enable Feature", command=on_checkbox_click)
      checkbox.pack()
      
      root.mainloop()
      
  6. Customizing Checkbutton appearance in Tkinter:

    • Description: The appearance of a Checkbutton can be customized using options like font, background color, foreground color, etc.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      checkbox = tk.Checkbutton(root, text="Customize", font=("Arial", 12), bg="lightgray", fg="blue")
      checkbox.pack()
      
      root.mainloop()
      
  7. Handling Checkbutton state changes in Tkinter:

    • Description: You can handle the state changes of a Checkbutton by associating a callback function with the command option.
    • Code:
      import tkinter as tk
      
      def on_checkbox_change():
          state = var.get()
          print("Checkbox state:", state)
      
      root = tk.Tk()
      
      var = tk.IntVar()
      
      checkbox = tk.Checkbutton(root, text="Enable Feature", variable=var, command=on_checkbox_change)
      checkbox.pack()
      
      root.mainloop()
      
  8. Building a form with Tkinter Checkbuttons:

    • Description: Checkbuttons are commonly used in forms to allow users to select multiple options.
    • Code:
      import tkinter as tk
      
      def submit_form():
          selected_options = [var1.get(), var2.get()]
          print("Selected options:", selected_options)
      
      root = tk.Tk()
      
      var1 = tk.IntVar()
      var2 = tk.IntVar()
      
      checkbox1 = tk.Checkbutton(root, text="Option 1", variable=var1)
      checkbox2 = tk.Checkbutton(root, text="Option 2", variable=var2)
      
      submit_button = tk.Button(root, text="Submit", command=submit_form)
      
      checkbox1.pack()
      checkbox2.pack()
      submit_button.pack()
      
      root.mainloop()