The Wayback Machine - https://web.archive.org/web/20240926141721/https://www.geeksforgeeks.org/python-how-to-dynamically-change-text-of-checkbutton/
Open In App

Python | How to dynamically change text of Checkbutton

Last Updated : 02 Apr, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Tkinter is a GUI (Graphical User interface) module which is used to create various types of applications. It comes along with the Python and consists of various types of widgets which can be used to make GUI more attractive and user-friendly. Checkbutton is one of the widgets which is used to select multiple options.

Checkbutton can be created as follows:

chkbtn = ttk.Checkbutton(parent, value = options, ...)

Code #1:




# This will import tkinter and ttk
from tkinter import * from tkinter import ttk
  
root = Tk()
  
# This will set the geometry to 200x100
root.geometry('200x100')
  
text1 = StringVar()
text2 = StringVar()
  
# These text are used to set initial
# values of Checkbutton to off
text1.set('OFF')
text2.set('OFF')
  
chkbtn1 = ttk.Checkbutton(root, textvariable = text1, variable = text1,
                          offvalue = 'GFG Not Selected',
                          onvalue = 'GFG Selected')
  
chkbtn1.pack(side = TOP, pady = 10)
chkbtn2 = ttk.Checkbutton(root, textvariable = text2, variable = text2,
                          offvalue = 'GFG Average',
                          onvalue = 'GFG Good')
chkbtn2.pack(side = TOP, pady = 10)
  
root.mainloop()


Output #1: When you run application you see the initial states of Checkbutton as shown in output.

Output #2: As soon as you select the Checkbutton you’ll see that text has been changed as in output.

Output #3: When you deselect the Checkbutton you’ll again observe following changes.

Code #2: Commands can be integrate with the Checkbutton which can be execute when checkbutton is selected or deselected depending upon conditions.




# Importing tkinter, ttk and
# _show method to display
# pop-up message window
from tkinter import * from tkinter import ttk
from tkinter.messagebox import _show
  
root = Tk()
root.geometry('200x100')
  
text1 = StringVar()
text1.set('OFF')
  
# This function is used to display
# the pop-up message
def show(event):
    string = event.get()
    _show('Message', 'You selected ' + string)
  
chkbtn1 = ttk.Checkbutton(root, textvariable = text1, variable = text1,
                          offvalue = 'GFG Good',
                          onvalue = 'GFG Great',
                          command = lambda : show(text1))
chkbtn1.pack(side = TOP, pady = 10)
  
root.mainloop()


Output:

Note: In above code offvalue and onvalue are used to set the values of Checkbutton of non-selected state and selected state respectively.



Previous Article
Next Article

Similar Reads

Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
Tkinter is a GUI (Graphical User Interface) module which comes along with the Python itself. This module is widely used to create GUI applications. tkinter.ttk is used to create the GUI applications with the effects of modern graphics which cannot be achieved using only tkinter. Checkbutton is used to select multiple options. Checkbuttons can be cr
2 min read
Python Tkinter - Checkbutton Widget
The Checkbutton widget is a standard Tkinter widget that is used to implement on/off selections. Checkbuttons can contain text or images. When the button is pressed, Tkinter calls that function or method. Note: For more reference, you can read our article, What is WidgetsPython Tkinter OverviewPython Tkinter TutorialTkinter Checkbutton Widget Synta
5 min read
How To Disable Checkbutton 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. In this article, we will learn about the Checkbutton widget in Python and how we can disable it so that it is uneditable. Steps to Disable Checkbutton in TkinterThe checkbutton can be
3 min read
How To Dynamically Resize Button Text in Tkinter?
Prerequisite: Python GUI – tkinter, Dynamically Resize Buttons When Resizing a Window using Tkinter In this article, we will see how to make the button text size dynamic. Dynamic means whenever button size will change, the button text size will also change. In Tkinter there is no in-built function, that will change the button text size dynamically.
3 min read
How to Dynamically Load Modules or Classes in Python
Python provides a feature to create and store classes and methods and store them for further use. The file containing these sets of methods and classes is called a module. A module can have other modules inside it. Note: For more information, refer to Python Modules Example: A simple example of importing a module is shown below in which, there are
3 min read
Create Classes Dynamically in Python
A class defines a collection of instance variables and methods to specify an object type. A class can be used to make as many object instances of the type of object as needed. An object is an identified entity with certain attributes (data members) and behaviours (member functions). Group of objects having similar characteristics and behaviour are
2 min read
Python SQLAlchemy - Get column names dynamically
In this article, we are going to see how to dynamically get the column names of a table in SQLAlchemy using Python. Used database for demonstration: So, our student table has 5 columns namely sno, name, dob, class, and section, and our task is to fetch all these column names in our Python code. First of all, we will import the sqlalchemy module, cr
2 min read
Dynamically Update Multiple Rows with PostgreSQL and Python
In this tutorial, we will explore how to dynamically update multiple rows in a PostgreSQL database using Python. By the end of this tutorial, you'll have a solid understanding of how to write efficient Python code that interacts with PostgreSQL to update multiple rows in a database. We'll cover topics such as connecting to a PostgreSQL database, ex
3 min read
wxPython - Dynamically enable and disable tools in toolbar using button
In this article we are going to learn how can we enable and disable dynamically using a single button. We will create a button and a toolbar with a single tool. We will use a normal button like a toggle button. Steps : 1. Create a toolbar in a frame. 2. Add a tool with a certain id. 3. Create button in the window. 4. Declare a global count variable
1 min read
Dynamically Resize Buttons When Resizing a Window using Tkinter
Prerequisite: Python GUI – tkinter Button size is static, which means the size of a button cannot be changed once it is defined by the user. The problem here is while resizing the window size, it can affect the button size problem. So the solution here is, make a dynamic button, which means the button size will change as per window size. Let's unde
2 min read
How to Dynamically Resize Background Images - Tkinter
You might surely be worried about how to resize your background image with the change in screen size. In this article, We are creating an advanced-level GUI application using the Tkinter module using Python in which you need to resize the app window various times and dynamically resize background images. Modules Required: Tkinter: As the GUI applic
6 min read
Dynamically Updating Plot In Matplotlib
This article shows how to dynamically update a Matplotlib (a data visualization library for Python programming language) plot as the data changes. It provides two methods to plot - first the API (useful for large programs and/or ones requiring deep control) and second the Pyplot interface (inspired by Matlab). In this article, we will show how to u
5 min read
Add Forms Dynamically In Django Using Formset And JavaScript
Formsets in Django allow us to manage a collection of forms as a single unit. They are particularly useful when we need to handle multiple forms on a single page, like adding or editing multiple objects at once. However, a common requirement in many applications is the ability to add forms to a formset without a page reloading dynamically. Table of
8 min read
Convert Text and Text File to PDF using Python
PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Document Format) is one of the basic requirements in
3 min read
Convert Text Image to Hand Written Text Image using Python
In this article, we are going to see how to convert text images to handwritten text images using PyWhatkit, Pillow, and Tesseract in Python. Module needed: Pytesseract: Sometimes known as Python-tesseract, is a Python-based optical character recognition (OCR) program. It can read and recognize text in photos, license plates, and other documents. To
2 min read
Python: Convert Speech to text and text to Speech
Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an introduction on how to make use of the SpeechRecognition and pyttsx3 library of Python.Installation required: Python Speech Recognition module: pip install speechrecognitionPyAudio: Use the f
3 min read
PyQt5 – How to change text of pre-existing label | setText method
In GUI applications there is need of displaying information this done using labels in PyQt5, but sometimes there is also a need of changing the text of the label, in this tutorial we will see how it can be done. setText() method is used to change the content of the label. Syntax : label.setText(Info) Argument : It takes string as an argument. Code
1 min read
PyQt5 – How to change font and size of Label text ?
A label is a graphical control element which displays text on a form. A label is generally used to identify a nearby text box or other widget. Some labels can respond to events such as mouse clicks, allowing the text of the label to be copied, but this is not standard user-interface practice. In this article, we will see how to change the font and
2 min read
PyQt5 – How to change the text of existing push button ?
In this article we will see how to change the text of existing push button. We know when we create a button, we set the text to it but sometimes, conditions occur in which re-usability of push button occur i.e button should do its normal operation just change the text of it, so it look different. In order to do this we use setText method. This meth
2 min read
PyQt5 – Change the text font and size in Push button
In this article we will see how to change the text style or size of Push Button.QPushButton is a simple button in PyQt, when clicked by a user some associated action gets performed. For adding this button into the application, QPushButton class is used.In order to set font we will use setFont method which takes QFont object as argument. Syntax : bu
2 min read
PyQt5 - How to change style and size of text Progress Bar ?
In this article we will see how to change the font style and the size of data which is inside the progress bar. We can set the text in progress bar using setFormat method. In order to change the font and the size we will use setFont method which take QFont object as argument and font and size will be changes. Syntax : bar.setFont(QFont(font_name, s
2 min read
PyQt5 – Change text in Radio Button on click
In this article we will see how to change the text in the radio button. When we create a radio button we set some text or even we can leave it blank, radio button is created with the help of QRadioButton class. We will create a radio button and a push button such that when push button is pressed, radio button text should get changed. In order to do
2 min read
PyQt5 - How to change the spacing between indicator and text part of Radio Button ?
In this article we will see how to change the spacing between the indicator and the text part of the radio button. Spacing is basically distance between the indicator and the text part. We can change the distance between them with the help of setStyleSheet method, this method help us to set the style sheet for the radio button. Below is the style s
1 min read
PyQt5 - Change text of pre-existing Radio button
In this article we will see how we can change the text of pre-existing radio button when we pressed the push button, radio button text can be changed with the help of setText method. In order to do this we have to do the following - 1. Create a radio button 2. Create a push button 3. Add action to the push button such that when button is pressed a
2 min read
Change the color of certain words in the tkinter text widget
Python has various options for Graphical User Interface (GUI) development. One of the options is Tkinter. Tkinter and Python together provide a faster way for GUI development. The Tk GUI toolkit provides an object-oriented interface. For creating GUI applications using Tkinter we have to follow a few steps – Import Tkinter module.Create the main wi
3 min read
wxPython - Change font for text present in Radio Box
In this article we are going to learn how can we change the font of textpresent inside the Radio box. In order to do this we will follow 3 steps: Step 1: Create a wx.Font object variable named f. Step 2: Create a Radio box. Step 3: Set f as font for Radio box using SetFont() method. Syntax: wx.RadioBox.SetFont(self, font) Parameters Parameter Input
2 min read
How to change the Tkinter label text?
Prerequisites: Introduction to tkinter Tkinter is a standard GUI (Graphical user interface) package for python. It provides a fast and easy way of creating a GUI application. To create a tkinter application: Importing the module — tkinterCreate the main window (container)Add any number of widgets to the main window.Apply the event Trigger on the wi
3 min read
How To Change The Text Color Using Tkinter.Label
In Tkinter, we can customize the styling of elements such as labels to enhance the appearance of our GUI applications. One common customization is changing the text color of a Label widget. In this article, we will explore different approaches to changing the text color using tkinter.label in Python. Change The Text Color Using Tkinter.LabelBelow a
2 min read
How to Set Text of Tkinter Text Widget With a Button?
Prerequisite: Python GUI – tkinter Text Widget is used where a user wants to insert multi-line text fields. In this article, we are going to learn the approaches to set the text inside the text fields of the text widget with the help of a button. Approach: Using insert and delete methodImport the Tkinter module.Create a GUI window.Create our text w
2 min read
How to wrap text within Tkinter Text Box?
In this article, we will see that how can we wrap the text in the TKinter Text-Box using the Tkinter module Called textWrap Module. The textwrap module can be used for wrapping and formatting plain text. This module provides formatting of the text by adjusting the line breaks in the input paragraph. Example 1: Firstly We will import the Tkinter Lib
2 min read
Practice Tags :