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
How to Get the Input From Tkinter Text Box? In Tkinter, the Text widget allows users to input multiple lines of text, making it perfect for scenarios where you need to collect large amounts of data, such as messages, notes or any other textual information. To retrieve the text that a user has entered into the Text widget, you can use the get(
4 min read
How to Create Checkbox in Kivymd-Python In this article, we will see how to add the Check box in our application using KivyMD in Python. KivyMD is a collection of Material Design compliant widgets which can be used with Kivy. Installation: To install these modules type the below command in the terminal. pip install kivy pip install kivym
3 min read
How to get the index of selected option in Tkinter Combobox? In this article, we will be discussing how to get the index of the selected options in the Tkinter Combobox. A Combobox is a combination of a Listbox and an Entry widget. This widget allows users to select one value from a set of values. Syntax: def get_index(*arg): Label(app, text="The value at in
3 min read
How to get a new API response in a Tkinter textbox? In this article, we will look into how to make an API request and then insert it into a Tkinter textbox using Python, Before moving further we will cover some basic concepts related to our article that we will use frequently. Request: Requests are required to play with the web. If you want to use AP
4 min read
How to get selected value from listbox in tkinter? Prerequisites: Tkinter, Listbox ListBox is one of the many useful widgets provided by Tkinter for GUI development. The Listbox widget is used to display a list of items from which a user can select one or more items according to the constraints. In this article, we'll see how we can get the selected
2 min read