forked from furas/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock-widget.py
More file actions
87 lines (58 loc) · 2.07 KB
/
Copy pathclock-widget.py
File metadata and controls
87 lines (58 loc) · 2.07 KB
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
#!/usr/bin/env python
#import sys
#
#if sys.version_info.major == 2:
# import Tkinter as tk
#else:
# import tkinter as
try:
import Tkinter as tk # Python 2.x
except:
import tkinter as tk # Python 3.x
from datetime import datetime, timedelta
# --- constants ---
# empty
# --- classes ---
class Clock(tk.Frame):
def __init__(self, parent, offset_minutes=0):
# create widget (and send all arguments to Frame, ie. parent)
tk.Frame.__init__(self, parent) # Python 2 & 3
#super().__init__() # Python 3 only
self.offset_minutes = timedelta(minutes=offset_minutes)
# create variable for displayed time and use it with Label
self.txt_var = tk.StringVar()
tk.Label(self, textvariable=self.txt_var).pack()
def update(self):
# update displayed time
current_time = datetime.now()
current_time += self.offset_minutes
current_time_str = current_time.strftime('%Y.%m.%d %H:%M:%S')
# current_time_str = datetime.now().strftime('%Y.%m.%d %H:%M:%S')
self.txt_var.set(current_time_str)
# update again after 1000ms (1s)
self.after(1000, self.update)
# ---
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title('World Times')
tk.Label(self, text="-120min.:").grid(row=0, column=0, sticky=tk.E)
tk.Label(self, text="Local:").grid(row=1, column=0, sticky=tk.E)
tk.Label(self, text="+120min.:").grid(row=2, column=0, sticky=tk.E)
self.clock_1 = Clock(self, -120)
self.clock_1.grid(row=0, column=1)
self.clock_2 = Clock(self)
self.clock_2.grid(row=1, column=1)
self.clock_3 = Clock(self, 120)
self.clock_3.grid(row=2, column=1)
def run(self):
# update first time
self.clock_1.update()
self.clock_2.update()
self.clock_3.update()
# start the engine :)
self.mainloop()
# --- functions ---
# empty
# --- main ---
App().run()