How to change the Tkinter label text

Read Time:8 Minute, 14 Second

Table of Contents

Tkinter label widgets can display text or a picture on the screen. You can use only one typeface on a label. It is possible to have many lines of text. For instance, a label can include any text, and a window can have numerous labels (just like any widget can be displayed multiple times in a window).

The label text attribute in Python Tkinter allows you to change/update the label text easily. Another technique to edit the Tkinter label text is to change the label’s text property. In this lesson, we’ll look at changing label text in Tkinter Python using more than one approach.

Tkinter is a python module that provides a standard GUI (Graphical user interface). It allows you to quickly and easily create a graphical user interface (GUI) application.

To make a tkinter application, follow these steps:

  • First import the module — tkinter
  • Make the primary window (container)
  • Add as many widgets as you want to the main window.
  • Apply the Trigger event to the widgets.

Widgets are the command and control elements of any graphical user interface (GUI) application. The primary purpose of this widget is to provide a wide range of controls. Buttons, labels, text boxes, and many other widgets are examples. Importing tkinter is similar to importing any other Python package. The module’s name in Python 2.x is ‘Tkinter,’ but in Python 3.x, it is ‘tkinter.’

SUGGESTED READ

When establishing a Python application with GUI, there are two primary approaches that the user must remember.

Tkinter provides the method Tk(screenName=None, baseName=None, className=’Tk’, useTk=1) for creating a main window. You can replace the className with the desired name to change the window’s name. The following is the basic code used to generate the application’s primary window:

where m is the main window object’s name

When your application is ready to run, you call the mainloop() method. mainloop() is an infinite loop that runs the application, waits for an event and processes the event as long as the window is open.

Tkinter also provides access to the widgets’ geometric configuration, which you can use to organize widgets in parent windows. There are three types of geometry manager classes.

SUGGESTED READ

  • pack() method: It arranges the widgets into blocks before placing the widgets in the parent widget.
  • grid() method: Before placing the widgets in the parent widget, it organizes them in a grid (table-like structure).
  • The method place(): It arranges the widgets by placing them in the program’s specified locations.

The label is one of its widgets, and it’s in charge of creating a display box area for text and graphics. Let’s first explore the Tkinter label widget.

Python Tkinter – Label

Python provides many alternatives for creating a graphical user interface (Graphical User Interface). Further, Tkinter is the most widely used GUI technique out of all the options. It’s a standard Python interface to the Python-supplied Tk GUI toolkit. Python with Tkinter is the quickest and most straightforward approach to constructing graphical user interfaces.

Using widgets, creating a GUI with Tkinter is simple. Widgets, such as buttons and menus, are standard graphical user interface (GUI) elements. Now, let’s look at how to update the label’s text:

The Label Widget

Tkinter Label is a widget that allows you to create display boxes with text or graphics. The developer can change the text displayed by this widget at any moment. You can also use it to execute operations like underlining text and spanning text across numerous lines. It’s vital to remember that a label can only display text in one typeface at a time. To utilize a label, you have to tell it what to display (this can be text, a bitmap, or an image).

The syntax is as follows:

SUGGESTED READ

The master parameter represents the parent window.

Parameters

  • master: This is the window’s parent.
  • Options: The format of the widget can be changed using various settings. The most commonly used options for this widget are listed below. These options can be used as comma-separated key-value pairs:

The following are a few options:

anchor: If the widget has more space than required for the text, this option is used to manage the alignment of the text. anchor=CENTER is the default, which centered the text in the available space.

bg: This option changes the background color behind the label and indicator.

height: This option is used to specify the new frame’s vertical dimension.

SUGGESTED READ

width: The label’s width in characters (not pixels!). If this option is not selected, the label is scaled to fit its contents.

bd: This option is used to control the size of the indicator’s border. Also, the default bd value is 2 pixels.

font: If you’re using the text or textvariable options to show text in the label, the font option is used to select the typeface displayed in the text.

cursor: When the mouse is moved over the label, it is used to designate which cursor should be displayed. The standard cursor is used by default.

bitmap: It is used to set the bitmap to the provided graphical object so that the label can represent graphics rather than text.

SUGGESTED READ

fg: Text and bitmap labels use the label clear. The operating system determines the default.
It is the color that will appear at the place of the 1-bits in a bitmap if it is displayed.

image: In the label widget, this option displays a static image.

padx: This option adds extra spaces between the text on the label’s left and right sides. This option has a default value of 1.

pady: This option adds extra space between the label’s top and bottom text. This option has a default value of 1.

Justify: This option is used to specify how many lines of text should be aligned. Use the values LEFT, RIGHT, or CENTER. It’s worth noting that the anchor option is used to position the text within the widget. CENTER is the default setting for justify.

SUGGESTED READ

relief: The appearance of a decorative border around the label is controlled by this option. FLAT is the default setting for this parameter.

underline: This

wraplength: Instead of having a single line for the label text, it can be split across multiple lines, each with the number of characters indicated in this option.

Example: using a Label in Tkinter

Now, let’s look at how to update the label’s text:

Using Label.config() method

The syntax for Label.config() is as follows:

SUGGESTED READ

Parameter: text– The text to display in the label.

This method is used to do a label widget overwriting.

Example:

Using StringVar() class

StringVar is a type of Tkinter constructor that creates a string variable in Tkinter. Tkinter will update this particular widget when the StringVar variable is adjusted when we pair it with the Tkinter widgets.

The syntax for StringVar() is as follows:

SUGGESTED READ

Return: String variable object

This class is used to set and change values per the requirements.

Example 1 : Using StringVar() class

Example 2: Using StringVar() class

The string variable could not be started by the Tkinter constructor using the string.

To set the StringVar value, we should use the set method, such as self.text.set (“Test”).

SUGGESTED READ

By setting textvariable to self.text associates the StringVar variable self.text with the label widget self.label. If self.text is changed, the Tk toolkit starts tracking the changes and updates the text self.label. A Tkinter dynamic label is created with the code above. When the self.text is changed, it immediately displays the Tkinter label text.

Use the label text property to change/update the Python Tkinter Label Text

Changing the label’s text property is another way to change the Tkinter label text.

The text of the label object can be started with text=”Text” and altered by setting a new value to the text key of the label object. We may alternatively use the tk.Label.configure() method to update the text property, as seen below. The above codes function in the same way.

You can update the text of the label widget using a button and a function if you need to tweak or change it dynamically.

Output

SUGGESTED READ

update the text of the label widget using a button and a function

When you run the code above, a label text and a button will appear in the window. The label text will only be updated when you click the button.

after clicking on "Update Label"
after clicking on “Update Label”

Example: font configuration

Some Tk widgets, such as the label, text, and canvas widgets, allow you to choose the typefaces used to display text. A “font” configuration option is commonly used to accomplish this. It’s crucial to keep in mind that typefaces are a few areas that aren’t platform-independent. The attribute “fg” is used to modify the color of the text. On the other hand, the attribute “bg,” can be used to change the label’s background color.

Conclusion

Tkinter Label widgets are commonly used in applications to show text or images. Using the config(**options) method, we can change the label widget’s text property, color, background, and foreground colors. If you need to adjust or change the text of the label widget dynamically, you can use a button and a function to do so.

Source: https://www.codeunderscored.com/how-to-change-the-tkinter-label-text/

WP Ad Inserter plugin for WordPress