How to Add Text to a Label on Python

A label is an object created using the "Tkinter" module in Python, which allows you to create graphical user interfaces (GUIs). Label objects are used to display text and images. There's a bit of overhead involved in creating an interface with the "Tkinter" module, but once you get started you can easily update a text label using its "configure" method.

Instructions

    • 1

      Use the following commands to initialize a GUI:

      from tkinter import *

      root = Tk()

      myLabel = Label(root, text='My text.')

      myLabel.pack()

      The second argument of the label's constructor ("text=...") is optional. When these commands are finished the label is visible in a new window.

    • 2

      Replace the label's current text with new text using the "configure" method:

      myLabel.configure(text='New Text.')

    • 3

      Append text to the end of a label by using the "cget" method and the concatenation operator ("+") within the "configure" method:

      myLabel.configure(text=myLabel.cget('text')+' APPENDED TEXT')

Related Searches:

Comments

Related Ads

Featured