Monday, September 23, 2024
Google search engine
HomeLanguagesHow to remove text from a label in Python?

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 Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest, most reliable and easiest way to create a desired GUI application. 

The Task is to remove the text from label, once text is initialized in Tkinter.

Approach:

  • Import module
  • Create a normal Tkinter window.
  • Add Label and Create One Button

Syntax:

Text(Object Name,text=”Enter Text”, **attr)

  • For remove the text, we will use config() method in Tkinter

config is used to access an object’s attributes after its initialization.

Syntax:

Object_Name.config(**attr)

Given below is the program to implement the same:

Python3




# Import Module
from tkinter import *
 
# Create Object
root = Tk()
 
# specify size of window.
root.geometry("400x400")
 
# Remove text from label
 
def remove_text():
    label.config(text="")
 
# Create Label
label = Label(root, text="Hello World!", font="BOLD")
label.pack()
 
# Create Delete Button
Button(root, text="Delete", command=remove_text).pack()
 
# Execute Tkinter
root.mainloop()


Output:

Output:before clicking delete

Output:after clicking delete

RELATED ARTICLES

Most Popular

Recent Comments