tkinter while button not pressed 1

tkinter while button not pressed

from tkinter import *

root = Tk()
root.geometry('250x250')

display = Label(root, text='Hello', font=('', 20))
display.pack(pady=40)

def restart():
    display['text'] = 'Restarting...'
    but['state'] = 'disable'            # Once restarted the button gets disabled

def cancel():
    # Cancel the current after with it id
    root.after_cancel(L)
    display['text'] = 'Cancelled'

# Take a reference of after
L = root.after(5000, restart)

but = Button(root, text='Cancel', command = cancel )
but.pack(side='bottom', pady=30)

mainloop()

Here is what the above code is Doing:
1. We are creating a label and a button.
2. We are using the after method to call the restart function after 5 seconds.
3. We are using the after_cancel method to cancel the after method.
4. We are disabling the button once the restart function is called.

Similar Posts