1
+ import tkinter as tk
2
+ import tkinter .font as TkFont
3
+ from datetime import datetime
4
+
5
+ def timer ():
6
+ global work
7
+ if work :
8
+ now = str (txt_var .get ())
9
+ m ,s = map (int , now .split (":" ))
10
+ m = int (m )
11
+ s = int (s )
12
+ if (s < 59 ):
13
+ s += 1
14
+ elif (s == 59 ):
15
+ s = 0
16
+ if (m < 59 ):
17
+ m += 1
18
+ elif (m == 59 ):
19
+ m = 0
20
+ if (m < 10 ):
21
+ m = str (0 )+ str (m )
22
+ else :
23
+ m = str (m )
24
+ if (s < 10 ):
25
+ s = str (0 )+ str (s )
26
+ else :
27
+ s = str (s )
28
+ now = m + ":" + s
29
+
30
+ txt_var .set (now )
31
+ if work :
32
+ root .after (1000 ,timer )
33
+ #start function
34
+ def start ():
35
+ global work
36
+ if not work :
37
+ work = True
38
+ timer ()
39
+
40
+ #stop function
41
+ def pause ():
42
+ global work
43
+ work = False
44
+
45
+ #reset function
46
+ def reset ():
47
+ global work
48
+ if not work :
49
+ txt_var .set ('0:00' )
50
+
51
+
52
+ if __name__ == "__main__" :
53
+ work = False
54
+
55
+ root = tk .Tk ()
56
+ root .geometry ("500x400" ) #width x height
57
+ root .title ("My StopWatch" )
58
+
59
+ txt_var = tk .StringVar ()
60
+ txt_var .set ('0:00' )#initial display of string
61
+ root .config (background = "lavender" )
62
+
63
+ fontstyle = TkFont .Font (family = "Helvetica" ,size = 60 ,)
64
+ tk .Label (root ,textvariable = txt_var ,font = fontstyle ,).pack ()
65
+
66
+ #creating the buttons for start,stop and reset
67
+ T = tk .Text (root ,height = 0.7 ,width = 9 )
68
+ T .pack ()
69
+ T .insert (tk .END ," mm : ss " )
70
+ tk .Button (root ,text = "Start" ,command = start ,bg = 'misty rose' ).pack (fill = 'x' )
71
+ tk .Button (root ,text = 'Pause' ,command = pause ,bg = 'misty rose' ).pack (fill = 'x' )
72
+ tk .Button (root ,text = 'Reset' ,command = reset ,bg = 'misty rose' ).pack (fill = 'x' )
73
+ tk .Button (root ,text = 'Exit' ,command = root .destroy ,bg = 'misty rose' ).pack (fill = 'x' )
74
+ root .mainloop ()
0 commit comments