w3resource

Python tkinter widgets Exercise: Create a Checkbutton widget using tkinter module

Python tkinter widgets: Exercise-5 with Solution

Write a Python GUI program to create a Checkbutton widget using tkinter module.

Sample Solution:

Python Code:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
my_boolean_var = tk.BooleanVar()

my_checkbutton = ttk.Checkbutton(
    text="Check when the option True",
    variable=my_boolean_var
)
my_checkbutton.pack()
root.mainloop()

Explanation:

In the exercise above -

  • import tkinter as tk - Import the Tkinter library (tkinter).
  • from tkinter import ttk - Import the themed Tkinter library (ttk).
  • root = tk.Tk() - Create the main Tkinter window (root).
  • my_boolean_var = tk.BooleanVar() - Create a BooleanVar (my_boolean_var) to hold a boolean variable.
  • my_checkbutton = ttk.Checkbutton(text="Check when the option True", variable=my_boolean_var) - Create a Checkbutton widget (my_checkbutton) using ttk.Checkbutton. Set the text for the Checkbutton. Associate the BooleanVar (my_boolean_var) with the Checkbutton.
  • my_checkbutton.pack() - Display the Checkbutton within the main window.
  • root.mainloop() - Start the Tkinter main loop to run the GUI application.

Sample Output:

Tkinter: Create a Checkbutton widget using tkinter module

Flowchart:

Flowchart: Create a Checkbutton widget using tkinter module

Python Code Editor:


Previous: Write a Python GUI program to create a Combobox with three options using tkinter module.
Next: Write a Python GUI program to create a Spinbox widget using tkinter module.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/tkinter/python-tkinter-widgets-exercise-5.php