tkinter while button not pressed 1

tkinter while button not pressed

from tkinter import *

root = Tk()

display = Label(root, text='Hello', font=('', 20))

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

def cancel():
    # Cancel the current after with it id
    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)


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