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 Clear Out A Frame In The Tkinter?
Tkinter is a module in Python which is used to create GUI applications. It has many useful widgets such as Label, Button, Radiobutton, Checkbutton, Listbox, and more. Frame is a container in Tkinter within which many types of widgets and many numbers of widgets can be present. All the tkinter widget
2 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 ind
3 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 kivymd
3 min read
How to create Option Menu in Tkinter ?
The Tkinter package is the standard GUI (Graphical user interface) for python, which provides a powerful interface for the Tk GUI Toolkit. In this tutorial, we are expecting that the readers are aware of the concepts of python and tkinter module. OptionMenu In this tutorial, our main focus is to cre
3 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
How to Create a Checkbox in HTML?
The checkbox is the HTML form element that lets users select one or more options from predefined choices. It can often be used when a user selects multiple items in the list. Checkboxes can be checked or unchecked, and they are created using the <input> tag with the type attributes set to the
3 min read
How to Get the Value of an Entry Widget in Tkinter?
Tkinter, a standard GUI (Graphical User Interface) toolkit for Python, offers various widgets to create interactive applications. Among these widgets, the Entry widget allows users to input text or numerical data. Often, developers need to retrieve the value entered by the user in an Entry widget fo
2 min read
How to close only the TopLevel window in Python Tkinter?
In this article, we will be discussing how to close only the TopLevel window in Python Tkinter. Syntax: def exit_btn(): top.destroy() top.update() btn = Button(top,text='#Text of the exit button',command=exit_btn) Stepwise Implementation Step 1: First of all, import the library tkinter. from tkinter
3 min read
How to remove text from a label in Python?
Prerequisite: Python GUI â tkinter In this article, the Task is to remove the text from label, once text is initialized in Tkinter. Python offers multiple options for developing GUI (Graphical User Interface) out of which Tkinter is the most preferred means. It is a standard Python interface to the
1 min read