Open In App

How To Get The Input From A Checkbox In Python Tkinter?

Last Updated : 28 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

get_value_from_checkbox_tkinter_pic_1
GUI window

Now click on the checkbutton to see the result in the GUI.

get_value_from_checkbox_tkinter_pic_2
The checkbutton is checked now.

Now again uncheck the checkbutton.

get_value_from_checkbox_tkinter_pic_3
Checkbutton unchecked

Conclusion

In this article, we have discussed about how to get input from a checkbox in Tkinter.


Next Article
Article Tags :
Practice Tags :

Similar Reads