I want to draw a widget (in this example, a canvas) and then remove it after some time. Like a message that shows up and then is removed, just so that the user can read it, but there is no need to click “ok” or something like that to remove the message.
Here is an example code.
from tkinter import*
root = Tk()
canvas_1 = Canvas(root, width = 300, height = 300, bg = 'white')
canvas_1.grid(column = 0, row = 0)
canvas_2 = Canvas(canvas_1, width = 200, height = 200, bg = 'blue')
canvas_2.place(x = 50, y = 50)
canvas_1.after(1000, canvas_2.place_forget())
root.mainloop()
The problem is that it seems tkinter runs the after() method before everything else, no matter when it is called in the code. The result is that the canvas_2 never appears.
I’ve tried time.sleep() but it seems to work in the same way in this case.
Thanks in advance.
The issue is that your
afterstatement is actually causingcanvas_2to be forgotten immediately. This is because the ()’s are telling Python to run theplace_forgetfunction (rather than run it in 1000ms). Remove the ()’s and you will be good to go. Best of luck.Replace this:
with this: