World Clock syntax error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: World Clock syntax error (/thread-42066.html) |
World Clock syntax error - OscarBoots - May-03-2024 Hi Forum, I'm using tis code to create a World clock for 4 Countries. I've copied the code and understand the logic, but i must have made an error as it doesn't show a frame with 4 different Countries timezones. Can anyone see why this isn't working? Thanks. # https://www.google.com/search?sca_esv=08156674d0b13f07&sca_upv=1&rlz=1C1VDKB_enAU1081AU1105&cs=0&sxsrf=ADLYWIKKYe2lYbhYyIu46OSDBaPbekhPFg:1714698269306&q=World+clock+in+Python&sa=X&ved=2ahUKEwi0uO7ppPCFAxVcd2wGHYRwCFMQpboHegQIARAA&biw=1920&bih=911&dpr=1#fpstate=ive&vld=cid:10837df9,vid:zFCp7iczAPk,st:0 from datetime import datetime import pytz from tkinter import * import time root = Tk() root.geometry("500x250") def times(): home=pytz.timezone('Australia/Victoria') local_time=datetime.now(home) current_time=local_time.strftime("%H:%M:%S") clock.config(text=current_time) name.config(text="Australia") clock.after(200,times) home = pytz.timezone('Asia/Kolkata') local_time = datetime.now(home) current_time = local_time.strftime("%H:%M:%S") clock1.config(text = current_time) name1.config(text = "India") home = pytz.timezone('America/New_York') local_time = datetime.now(home) current_time = local_time.strftime("%H:%M:%S") clock2.config(text = current_time) name2.config(text = "U.S.A") home = pytz.timezone('UTC') local_time = datetime.now(home) current_time = local_time.strftime("%H:%M:%S") clock3.config(text = current_time) name3.config(text = "UTC") clock.after(200,times) # Australian Time AEST name = Label(root,font = ("times",20,"bold")) name.place(x = 30, y = 5) clock = Label(root,font = ("times",15,"bold")) clock.place(x = 10, y = 40) nota = Label(root, text = "Hours Minutes Seconds",font = "times 10 bold") nota.place(x = 10, y = 80) # Indian Time IST name1 = Label(root, font = ("times",20,"bold")) name1.place(x = 330, y = 5) clock1 = Label(root, font = ("times",25,"bold")) clock1.place(x = 310, y = 40) nota1 = Label(root, text = "Hours Minutes Seconds",font = "times 10 bold") nota1.place(x = 310, y = 80) # American Time name2 = Label(root,font = ("times",20,"bold")) name2.place(x = 30, y = 105) clock2 = Label(root, font = ("times",25,"bold")) clock2.place(x = 10, y = 140) nota2 = Label(root, text = "Hours Minutes Seconds",font = "times 10 bold") nota2.place(x = 10, y = 180) # UTC name3 = Label(root,font = ("times",20,"bold")) name3.place(x = 330, y = 105) clock3 = Label(root, font = ("times",25,"bold")) clock3.place(x = 310, y = 140) nota3 = Label(root, text = "Hours Minutes Seconds",font = "times 10 bold") nota3.place(x = 310, y = 180) root.mainloop() RE: World Clock syntax error - snippsat - May-03-2024 clock.after(200, times) you call two times,only the last one needed.Also most call times before mainloop. times() root.mainloop()Also should not use pytz anymore.Quote:This project is in maintenance mode. I would advise to use pendulum,it handle the Timezones the best. Here is some improvement(your whitespace after every line is a little annoying😕) a class make more sense when use a GUI and i use pendulum. import tkinter as tk from tkinter import ttk import pendulum class App(tk.Tk): def __init__(self): super().__init__() self.title("World Clock") self.geometry("600x500") # Define the timezones you want to display self.timezones = { 'Australia (Victoria)': 'Australia/Melbourne', 'India (Kolkata)': 'Asia/Kolkata', 'USA (New York)': 'America/New_York', 'Coordinated Universal Time': 'UTC' } self.labels = {} self.create_widgets() self.update_clock() def create_widgets(self): for idx, (label, tz) in enumerate(self.timezones.items()): row = idx * 2 # Timezone and Clock labels tk.Label(self, text=label, font=("times", 20, "bold")).grid(row=row, column=0, pady=10, sticky="ew") clock_label = tk.Label(self, font=("times", 25, "bold"), anchor="center") clock_label.grid(row=row + 1, column=0, pady=5, sticky="ew") self.labels[tz] = clock_label tk.Label(self, text="Hours Minutes Seconds", font="times 10 bold").grid(row=row + 2, column=0, sticky="ew") self.grid_columnconfigure(0, weight=1) def update_clock(self): for tz, label in self.labels.items(): local_time = pendulum.now(tz) current_time = local_time.strftime("%H:%M:%S") label.config(text=current_time) # Schedule the next update self.after(1000, self.update_clock) if __name__ == "__main__": app = App() app.mainloop() |