-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnates_clock.py
More file actions
93 lines (76 loc) · 3.05 KB
/
nates_clock.py
File metadata and controls
93 lines (76 loc) · 3.05 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
88
89
90
91
92
93
import tkinter as tk
import pytz
from tkinter import Menu, StringVar
from datetime import datetime
class ClockApp:
def __init__(self):
self.root = tk.Tk()
self.root.title("Clock")
self.label = tk.Label(self.root, font=("Arial", 48), bg="black", fg="lime")
self.label.pack(padx=20, pady=20)
# Display current timezone
self.timezone_var = StringVar(value="UTC")
self.timezone_label = tk.Label(
self.root, text=f"Timezone: {self.timezone_var.get()}", font=("Arial", 14)
)
self.timezone_label.pack(pady=(0, 10))
self.timezones = sorted(pytz.all_timezones)
# Search box for timezones
search_frame = tk.Frame(self.root)
search_frame.pack(pady=(0, 10))
tk.Label(search_frame, text="Search Timezone:").pack(side=tk.LEFT)
self.search_var = StringVar()
self.search_entry = tk.Entry(search_frame, textvariable=self.search_var)
self.search_entry.pack(side=tk.LEFT)
self.search_entry.bind("<KeyRelease>", self.on_search)
# Listbox for filtered timezones
self.listbox = tk.Listbox(self.root, height=8, width=40)
self.listbox.pack()
self.listbox.bind("<<ListboxSelect>>", self.on_listbox_select)
self.update_listbox(self.timezones)
# Menubar
menubar = Menu(self.root)
timezone_menu = Menu(menubar, tearoff=0)
for tz in self.timezones:
timezone_menu.add_radiobutton(
label=tz,
variable=self.timezone_var,
value=tz,
command=self.update_clock,
)
menubar.add_cascade(label="Timezone", menu=timezone_menu)
self.root.config(menu=menubar)
def update_listbox(self, timezones):
self.listbox.delete(0, tk.END)
for tz in timezones:
self.listbox.insert(tk.END, tz)
def on_search(self, event=None):
search_term = self.search_var.get()
filtered = self.search_timezones(search_term)
self.update_listbox(filtered)
def on_listbox_select(self, event):
selection = self.listbox.curselection()
if selection:
tz = self.listbox.get(selection[0])
self.timezone_var.set(tz)
self.update_clock()
def search_timezones(self, search_term: str) -> list[str]:
"""Search for timezones containing the search term."""
return [tz for tz in self.timezones if search_term.lower() in tz.lower()]
def update_clock(self) -> None:
tz = pytz.timezone(self.timezone_var.get())
now = datetime.now(tz)
current_time = now.strftime("%H:%M:%S")
second = now.second
color = "red" if second % 2 == 0 else "lime"
self.label.config(text=current_time, fg=color)
self.timezone_label.config(text=f"Timezone: {self.timezone_var.get()}")
self.root.after(1000, self.update_clock)
def run(self):
self.update_clock()
self.root.mainloop()
def main():
app = ClockApp()
app.run()
if __name__ == "__main__":
main()