Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
World Clock syntax error
#1
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()
Reply
#2
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.
Projects using Python 3.9 or later are best served by using the timezone functionally.
now included in core Python and packages that work with it such as tzdata.

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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,394 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 471 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,795 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,320 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,388 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,356 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 966 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,967 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Hello World! rufenghk 0 999 Jun-02-2022, 09:48 PM
Last Post: rufenghk
  Mysql Syntax error in pymysql ilknurg 4 2,442 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020