My not OO version of timer for work and pause time (but it does use attributes for functions, which are objects, to avoid global declarations in functions).
Work and pause timer
from datetime import datetime, timedelta
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
def whole_seconds(time):
return str(time).split('.')[0]
def update():
if not pause_toggle.paused:
timer.set(whole_seconds(datetime.now() - start_time - pause_timer.pause_time))
else:
this_pause = datetime.now() - pause_toggle.pause_start
pause_timer.set(whole_seconds(this_pause + pause_timer.pause_time))
root.after(100,update)
def pause_toggle():
if pause_toggle.paused:
pause_button['text'] = 'Start pause'
pause_timer.pause_time += datetime.now() - pause_toggle.pause_start
else:
pause_button['text'] = 'Start work'
pause_toggle.pause_start = datetime.now()
pause_toggle.paused = not pause_toggle.paused
pause_toggle.paused = False
def make_label(text):
tk.Label(root, text=text, font=("Helvetica", 16)).pack()
text_var = tk.StringVar()
tk.Label(root, textvariable=text_var, font=("Helvetica", 16)).pack()
return text_var
root = tk.Tk()
root.title('Work and pause time')
root.geometry('240x180')
root.wm_attributes("-topmost", 1)
timer, pause_timer = make_label("Work time"), make_label("Pause time")
pause_button = tk.Button(root, text='Start pause', font=("Helvetica", 12, "bold"), command=pause_toggle)
pause_button.pack(side=tk.BOTTOM, pady=10)
pause_timer.pause_time = timedelta(0)
pause_timer.set(whole_seconds(pause_timer.pause_time))
start_time = datetime.now()
update()
root.mainloop()
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.