How To Get The Input From A Checkbox In Python Tkinter?
Last Updated :
28 Jun, 2024
The checkbox is called Checkbutton in Tkinter. Checkbutton allows users to choose more than 1 choice under a category. For example, we need not have only one choice when ordering food from a restaurant. We can choose 2 or 3 or more foods to order from the restaurant. In those applications, we use the check button in Tkinter.
How to create a Checkbox in Tkinter?
Setting Up Your Environment
Before diving into the code, make sure you have Python installed on your system. Tkinter comes pre-installed with Python, so you don't need to install any additional libraries.
Importing Tkinter
First, you need to import the Tkinter module. You can do this by adding the following line at the beginning of your Python script:
import tkinter as tk
from tkinter import ttk
Creating the Main Window
The next step is to create the main window for your application. This is done by creating an instance of the Tk class:
Python
root = tk.Tk()
root.title("Checkbox Example")
root.geometry("300x200")
Adding a Checkbox
To add a checkbox, you need to use the Checkbutton
widget. This widget requires a few parameters, including the parent window, text to display, and a variable to hold the state of the checkbox.
Python
check_var = tk.BooleanVar()
checkbox = tk.Checkbutton(root, text="Accept Terms and Conditions",
variable=check_var)
checkbox.pack(pady=20)
Retrieving the Checkbox State
To get the state of the checkbox (whether it's checked or not), you can simply read the value of the associated variable. Here’s how you can do it with a button click:
Python
def get_checkbox_state():
if check_var.get():
print("Checkbox is checked")
else:
print("Checkbox is unchecked")
submit_button = tk.Button(root, text="Submit",
command=get_checkbox_state)
submit_button.pack(pady=10)
How to Get input from a Checkbox in Tkinter?
To get the input from checkbutton, an option 'variable' is necessary. It has to be assigned to any variable such as IntVar() or StringVar() or BoolVar(). Consider a variable 'choice_var' is created to assign to checkbutton. If the onvalue and offvalue options of checkbutton is 1 and 0, then the variable 'choice_var' must be IntVar(). The onvalue and offvalue option can be assigned any string value also, then the variable 'choice_var' must be StringVar() type.
Example
In this example, we create a checkbutton to get the value from checkbutton whether it is clicked or not using integer values. The 'choice_var' is assigned IntVar().
Code
Python
# Importing tkinter
import tkinter as tk
# Function to get the value from the checkbutton on checking or unchecking it.
def choice():
# Checkbutton is checked.
if choiceNum.get()==1:
result_label.config(text="You ordered for Pizza")
else: # When checkbutton is unchecked.
result_label.config(text="You do not want PIZZA!")
# GUI
window = tk.Tk()
window.title("Geeksforgeeks")
window.geometry("300x200")
window.config(bg="green")
# variable to listen to checkbutton
choiceNum = tk.IntVar()
label1 = tk.Label(window,text="Want Pizza?",font=("Arial",13),
bg="green",fg="white")
label1.pack()
# Checkbutton
chkbtn = tk.Checkbutton(window,text="Click to Order",
command=choice,onvalue=1,
offvalue=0,variable=choiceNum)
chkbtn.pack()
# Label to display the result.
result_label = tk.Label(window,fg="white",bg="green",font=("Arial",15))
result_label.pack()
window.mainloop()
Output:
GUI windowNow click on the checkbutton to see the result in the GUI.
The checkbutton is checked now.Now again uncheck the checkbutton.
Checkbutton uncheckedConclusion
In this article, we have discussed about how to get input from a checkbox in Tkinter.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read