"Using the after Method"
# flash and beep every second using after() callback loop

'''
Of all the event tools in the preceding list, the after method may be the most interesting.
It allows scripts to schedule a callback handler to be run at some time in the future.

But after doesn’t pause the caller: callbacks are scheduled to occur in the background.
'''

from tkinter import *


class Alarm(Frame):
    def __init__(self, msecs=1000):                     # default = 1 second
        Frame.__init__(self)
        self.msecs = msecs
        self.pack()
        stopper = Button(self, text='Stop the beeps!!', command=self.quit)
        stopper.pack()
        stopper.config(bg='navy', fg='white', bd=8)
        self.stopper = stopper
        self.repeater()
    
    def repeater(self):                                 # on every N millisecs
        self.bell()                                     # beep now
        self.stopper.flash()                            # flash button now
        self.after(self.msecs, self.repeater)           # reschedule handler
    


if __name__ == '__main__': Alarm(msecs=1000).mainloop()
 