Как очистить label tkinter python
Перейти к содержимому

Как очистить label tkinter python

  • автор:

Deleting a Label in Python Tkinter

Tkinter label widgets are used to display text and images in the application. We can also configure the properties of Label widget that are created by default in a tkinter application.

If we want to delete a label that is defined in a tkinter application, then we have to use the destroy() method.

Example

In this example, we will create a Button that will allow the user to delete the label from the widget.

Output

If we run the above code, it will display a window with a label widget and a Button.

Как очистить label tkinter python

Thanks Larz! I have in my code a scrollbar and tk.canvas. Is there a way to erase/clear this field?

Hi folks. I have this code but when I click on the bottom ‘clear’ just the entry fields below are reseted and cleared:

Quote: first_name = tk.Entry(frame)
first_name.place(relwidth=0.65, relheight=0.5, relx=0.01)

last_name = tk.Entry(frame)
last_name.place(relwidth=0.65, relheight=0.5, relx=0.01, rely=0.5)

And I need to clear or restart/reset the function show_entry_fields() as well but I don’t know how can I do that.
Also when I do a new search the show_entry_fields need to be updated/renewed. Is it possible?

Python tkinter Label

We will add one tuple with font style using font option and add text color by using fg option. Change these lines. Tkinter window with font style and color

Adding background color ↑

add bg=’yellow’ to add background color to the Label Tkinter window with font background color
To change or manage the text of a label we can use StringVar(), by changing the value of the StringVar, we can update the text on the Label. Here by using set() method the StringVar() is assigned Hi Welcome, this can be updated to any other value to reflect the change in text of the Label.

overstrike font

Tkinter overstrike font

We can add other styles.

Alignment of text in Label ↑

Center aligned text in Label

We can use anchor attribute to align the text within a Label. Here we have given fixed width and height by using hight=13 and width =40 and background colour=’yellow’ to give more space for different alignments using anchor option.

Configure anchor option in Label

Default value for anchor is ‘center’.
Other values for anchor are n, ne, e, se, s, sw, w, nw, or center . These values are based on four direction North , South, East and West.
We can align text in all 9 directions by using 9 radioButtons. Here we are updating the anchor attribute by using config() method.

Entry box and Label ↑

Entry with Label anchor

For any entry widget we can use Label to indicate the type of data required. Here is one with anchor=’e’ to align the Label text close to the Entry widget.

Updating text of Label using user input ↑

Deleting Label ↑

Using relief option ↑

Option relief can take raised, sunken, ridge, solid,flat, groove Tkinter window label relief option

Positioning Labels using GRID ↑

To manage layout read more on grid() →
Tkinter Label positioning
We will change the font size, font colour, font family and place them at different locations using GRID row and column layouts. There are many optional options we can add to Label, the list is available below.

Changing the font colour dynamically ↑

Adding Image ↑

Window with Image

Enable disable Label ↑

disabled state

The state option can have three values : disabled, normal , active.
By using config the state can be managed. What is the difference between normal and active ?
With active state appearance can be changed to add mouse over effects like sunken ,raised etc..

Click event of Label ↑

Click event of Label

There is no command option for label. We can use bind option to trigger function on click of a Label. Here one child window is opened on click of the Label saying OK.

Creating Chess board using Label ↑

Chess board Tkinter

How to add Hyperlink in Tkinter window ↑

We will use one label to place hyper link or URL ( address of a web page ) on a Tkinter window.

Here we have connected mouse left button click event ( <Button-1> ) to the webbrowser.open_new() method.

Using Button as Hyperlink

Passing prameters through link

Link with parameter on Tkinter window

We will create the URL by asking user to enter name in one Entry box. On click of the link the site and particular page will open and it will display the welcome message by using the entered name.

How to delete or destroy Label in tkinter?

This Tkinter code doesn’t have a widget, just a label so it displays just a text on the screen so I want to destroy or delete the label after a certain time !. How can I do this when method label.after(1000 , label.destroy) doesn’t work?

2 Answers 2

Mike - SMT's user avatar

In the code you have provided I believe the fix you are looking for is to change this:

You need to destroy label.master (I am guessing this is actually a root window) because if you do not then you end up with a big box on the screen that is not transparent.

That said I am not sure why you are writing your app in this way. I guess it works and I was not actually aware you could do this but still I personally would write it using a root window to work with.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *