Home>

I am a python beginner.
Please let me know if there are any deficiencies.

Probably https://www.tutorialfor.com/go.php?id=45525

I want to erase text 3 seconds after displaying "something"
"Something" is shown as a label

Error message

1. Window opens->"Something" is displayed->After 3 seconds->"Something" disappears but I want to realize it
The window will open after 3 seconds.

2. In the first place, "something" is not displayed when the code is executed.

Error message
Applicable source code

import tkinter as tk
import time
root = tk.Tk ()

root.title (u "label") # Window label setting

root.geometry ("600x400") # Resize window

Res = tk.Label (root, text = "", font = (u "MS Mincho", 20), fg = "black")
Res.place (x = 110, y = 290)
Res ["text"] = "something" #after displaying something
time.sleep (3) # wait 3 seconds
Res ["text"] = "" # I want to erase something

root.mainloop ()

Comment out sleep → Something is not displayed.
-Comment out Res ["text"] = ""
→ A window appears after 3 seconds, and something appears

Supplemental information (FW/tool version etc.)

It's python 3.8.0

  • Answer # 1

    Time.sleep stops tkinter during that time.
    Use root.after to call back after a specified time and process it.

    import tkinter as tk
    import time
    root = tk.Tk ()
    root.title (u "label") # Window label setting
    root.geometry ("600x400") # Resize window
    Res = tk.Label (root, text = "", font = (u "MS Mincho", 20), fg = "black")
    Res.place (x = 110, y = 290)
    Res ["text"] = "something" #after displaying something
    def clear ():
        Res ["text"] = "" # I want to erase something
    root.after (3000, clear) clear in # 3 seconds
    root.mainloop ()