python tkinter 时间相关函数

2022-05-13 10:46:43 浏览数 (1)

image.png

image.png

image.png

image.png

image.png

一秒执行一次

flash and beep every second using after() callback loop

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()

代码语言:javascript复制
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()

消失隐藏

customize to erase or show button on after() timer callbacks

from tkinter import * import alarm

class Alarm(alarm.Alarm): # change alarm callback def init(self, msecs=1000): # default = 1 second self.shown = False alarm.Alarm.init(self, msecs)

代码语言:javascript复制
def repeater(self):                          # on every N millisecs
    self.bell()                              # beep now
    if self.shown:
        self.stopper.pack_forget()           # hide or erase button now
    else:                                    # or reverse colors, flash...
        self.stopper.pack()
    self.shown = not self.shown              # toggle state for next time
    self.after(self.msecs, self.repeater)    # reschedule handler

if name == 'main': Alarm(msecs=500).mainloop()

0 人点赞