1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| import tkinter as tk
import tkinter.font as font
import time
def create_clock(master=None):
clock = tk.Frame(master)
ignore = False
def on_configure(event):
nonlocal ignore
print ('on_configure')
size = label.winfo_width()
if ignore:
ignore = False
else:
_font['size'] = str(-size//6) # need rework
ignore = True
_font = font.Font(family="Tahoma", weight='bold', size=10)
label = tk.Label(clock, text='00:00:00', font=_font)
label.pack(fill='x', expand=1)
label.bind("<Configure>", on_configure)
_timer = None
_start_time = 0.0
def _update(delay=100):
nonlocal _timer
elapsed = time.time() - _start_time
m, s = divmod(elapsed, 60) # need rework
t = int(s)
s, ms = t, int((s - t)*100)
#print (m, s, ms)
label['text'] = '%02d:%02d:%02d' % (m, s, ms)
_timer = clock.after(delay, _update)
def _start():
nonlocal _timer, _start_time
if not _timer:
_start_time = time.time()
_update()
def _stop():
nonlocal _timer
if _timer:
clock.after_cancel(_timer)
_timer = None
def _reset():
nonlocal _start_time
_start_time = time.time()
clock.commands = {
'Start': _start,
'Reset' : _reset,
'Stop': _stop,
}
return clock
if __name__ == '__main__':
root = tk.Tk()
clock = create_clock(root)
clock.pack(fill='x', expand=1)
for label, action in clock.commands.items():
tk.Button(text=label, command=action).pack(side='left')
tk.Button(root, text='Quit', command=root.quit).pack(side='left')
tk.mainloop() |
Partager