Home > OS >  Tkinter Closing Frame After Button Click
Tkinter Closing Frame After Button Click

Time:08-02

I am trying to clear my Tkinter frame a few seconds after I click a button. Currently, this program works fine. It runs, and when the button is clicked, the frame is cleared. However, I want to make it so that after the button is clicked, the frame stays there for x number of seconds longer. After the time is up, the frame is cleared in the same way as it currently does (my clearFrame function). The commented line - I thought the .after would accomplish this but it just makes the popup label show after x seconds instead. I've seen this page but couldn't successfully apply it to what I want to do. Unlike what that page shows, I don't want to destroy my window or my frame, I just want to run my clearFrame function after x seconds.

#runs when the button is clicked, the button is on frame1
def click():
    top,  top_width, top_height = createFrame()
    createPopUpLabel(top, "Ok")
    autoClose(top, 5) #automatically close my pop up label
    #frame1.after(2000, clearFrame(frame1))
    clearFrame(frame1)

CodePudding user response:

frame1.after(2000, clearFrame(frame1))

after takes a callback function, which should just be a reference to a function e.g. clearFrame.

That's why your function just executes immediately, you need to pass lambda: clearFrame(frame1)

CodePudding user response:

def click():
    import time
    top,  top_width, top_height = createFrame()
    createPopUpLabel(top, "Ok")
    autoClose(top, 5) #automatically close my pop up label
    time.sleep(5) #Wait 5 seconds
    clearFrame(frame1)
  • Related