1

My program should destroy btn1 and create it again after one second in loop. I don't no why but my program only destroy btn1 and don't show this again. Anyone have idea why?

from tkinter import *
import random

def hide():
    btn1.destroy()
    btn1.after(2000,hide)

def show():
    btn1 = Button(root, bd=c, text="Hello\nWorld", relief="ridge", cursor="trek")
    btn1.grid(row=0,column=0)
    btn1.after(3000,show)

root = Tk()

root.geometry("350x150+400+400")

c=random.randint(20,40)

btn1 = Button(root, bd=c, text="Hello\nWorld", relief="ridge", cursor="trek")
btn1.grid(row=0,column=0)

btn1.after(2000,hide)
btn1.after(3000,show)

root.mainloop() 
0

1 Answer 1

2

It will work if you use grid_forget instead of creating a new object each time. Note that what happens at multiples of 6 seconds (2000 X 3000) depends on which one is the last one to execute.

def hide():
    btn1.grid_forget()
    btn1.after(2000,hide)

def show():
    btn1.grid(row=0,column=0)
    btn1.after(3000,show)

root = Tk()

root.geometry("350x150+400+400")

c=random.randint(20,40)

btn1 = Button(root, bd=c, text="Hello\nWorld",
              relief="ridge", cursor="trek")
btn1.grid(row=0,column=0)

btn1.after(2000,hide)
btn1.after(3000,show)

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.