[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

if sec != self.lastSec:
self.lastSec = sec
ampm = ((hour >= 12) and 'PM') or 'AM' # 0...23
hour = (hour % 12) or 12 # 12..11
self.display.onUpdate(hour, min, sec, ampm, self.cfg)
self.dateLabel.config(text=time.ctime(secsSinceEpoch))
self.countdownSeconds -= 1
if self.countdownSeconds == 0:
self.onCountdownExpire() # countdown timer
self.after(1000 // ChecksPerSec, self.onTimer) # run N times per second


3.x // trunc int div


def onCountdownSec(self, event):
secs = askinteger('Countdown', 'Seconds?')
if secs: self.countdownSeconds = secs


def onCountdownMin(self, event):
secs = askinteger('Countdown', 'Minutes')
if secs: self.countdownSeconds = secs * 60


def onCountdownExpire(self):


caveat: only one active, no progress indicator


win = Toplevel()
msg = Button(win, text='Timer Expired!', command=win.destroy)
msg.config(font=('courier', 80, 'normal'), fg='white', bg='navy')
msg.config(padx=10, pady=10)
msg.pack(expand=YES, fill=BOTH)
win.lift() # raise above siblings
if sys.platform[:3] == 'win': # full screen on Windows
win.state('zoomed')


###############################################################################


Standalone clocks


###############################################################################


appname = 'PyClock 2.1'


use new custom Tk, Toplevel for icons, etc.


from PP4E.Gui.Tools.windows import PopupWindow, MainWindow


class ClockPopup(PopupWindow):
def init(self, config=ClockConfig, name=''):
PopupWindow.init(self, appname, name)
clock = Clock(config, self)
clock.pack(expand=YES, fill=BOTH)


class ClockMain(MainWindow):
def init(self, config=ClockConfig, name=''):
MainWindow.init(self, appname, name)
clock = Clock(config, self)
clock.pack(expand=YES, fill=BOTH)


b/w compat: manual window borders, passed-in parent


PyClock: An Analog/Digital Clock Widget | 759
Free download pdf