forked from flavioamieiro/dojotimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdojotimer.py
executable file
·163 lines (134 loc) · 4.77 KB
/
dojotimer.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
DojoTimer - a simple timer for Coding Dojos.
Copyright (C) 2008 Flávio Amieiro <amieiro.flavio@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
If you find any bugs or have any suggestions email: amieiro.flavio@gmail.com
"""
from Tkinter import *
import tkSimpleDialog
class Clock(object):
"""
Main class for the app.
Takes as arguments to __init__:
master: the parent window
default_time: the desired default time
"""
def __init__(self, master, default_time=1):
# Create a frame
self.frame = Frame(master)
self.frame.pack()
# Get the TopLevel window
self.top = self.frame.winfo_toplevel()
# Change some of it's attributes
self.top.title("DojoTimer") # change the title
self.top.attributes('-topmost', 1) # make it always on top
self.top.resizable(0, 0) # make it unresizeable
# A separate method is responsible for creating the other widgets
self.__create_widgets()
# Some default values
self.running = False
self.default_time = default_time # defaul time (in minutes)
self.seconds = 60 * self.default_time
self.labelstr.set(
'%02d:%02d' % ((self.seconds /60), (self.seconds % 60))
)
def __create_widgets(self):
""" This function creates some widgets for the timer."""
# self.labelstr is going to be used as text in the label
# (which shows the time left). When this variable's value
# is modified (with the method .set('str')) the label
# changes on the fly.
self.labelstr = StringVar()
self.label = Label(
self.frame,
textvariable=self.labelstr,
fg='#198931',
font=('Helvetica', '48')
)
self.label.pack()
# Some buttons
self.start_btn = Button(self.frame, text="Start", command=self.start)
self.start_btn.pack(side=LEFT)
self.stop_btn = Button(self.frame, text='Pause', command=self.stop)
self.stop_btn.pack(side=LEFT)
self.reset_btn = Button(self.frame, text='Reset', command=self.reset)
self.reset_btn.pack(side=LEFT)
self.set_time_btn = Button(
self.frame,
text = 'Set time',
command = self.set_time,
)
self.set_time_btn.pack(side=LEFT)
self.quit_btn = Button(self.frame, text='Quit', command=self.frame.quit)
self.quit_btn.pack(side=LEFT)
def start(self):
"""
Start the clock
"""
if not self.seconds:
self.top.iconify()
self.reset()
if not self.running:
self.top.iconify()
self.running = True
self.top.title("*DojoTimer*")
self.update()
def update(self):
"""
Update the display
"""
if self.running:
if 0 < self.seconds <= 30:
self.label['fg'] = '#efbf16'
elif self.seconds <= 0:
self.top.deiconify()
self.label['fg'] = '#d70505'
self.stop()
new_str = '%02d:%02d' % ((self.seconds / 60), (self.seconds % 60))
self.labelstr.set(new_str)
self.label.after(1000, self.update)
if self.seconds:
self.seconds -= 1
def stop(self):
"""
Stop the clock
"""
self.top.title("DojoTimer")
self.running = False
def reset(self):
"""
Stop the clock and reset the time
"""
self.stop()
self.seconds = 60 * self.default_time
self.label['fg'] = '#198931'
new_str = '%02d:%02d' % ((self.seconds /60), (self.seconds % 60))
self.labelstr.set(new_str)
def set_time(self):
"""
Gets user input from a dialog and updates
self.default_time according to it
"""
try:
self.default_time = tkSimpleDialog.askfloat(
'Set time',
'Specify the time (in minutes)',
parent=self.top
)
self.reset()
except TypeError:
pass
if __name__ == '__main__':
root = Tk()
clock = Clock(root, 1)
root.mainloop()