Stop blink between clock updates

Viewed 33

Any way to make this stop blinking after every update of the clocks? this display a world time zone clock and every time it updates it blinks, i would like to remove that blinking, is there a way to do this?

from datetime import datetime
from tkinter import Tk, LabelFrame, Label
import pytz

root = Tk()

def timezones():
    global timezonelbl
    try:
        timezonelbl = LabelFrame(root, text="World Time Zones", padx=5, pady=5, highlightbackground="black", highlightthickness=2)
        timezonelbl.grid(row=0, column=0, sticky='nsew', pady=2)

        # list of desired countries
        Country_Zones = ['America/New_York', 'Asia/Kolkata', 'Australia/Sydney', 'Canada/Atlantic', 'Brazil/East',
                         'Chile/EasterIsland', 'Cuba', 'Egypt', 'Europe/Amsterdam',
                         'Europe/Athens', 'Europe/Berlin', 'Europe/Istanbul', 'Europe/Jersey', 'Europe/London',
                         'Europe/Moscow', 'Europe/Paris', 'Europe/Rome', 'Hongkong',
                         'Iceland', 'Indian/Maldives', 'Iran', 'Israel', 'Japan', 'NZ', 'US/Alaska', 'US/Arizona',
                         'US/Central', 'US/East-Indiana', 'Asia/Shanghai',
                         'Europe/Madrid', 'Asia/Urumqi', 'Brazil/Acre', 'Brazil/DeNoronha', 'Brazil/East',
                         'Brazil/West', 'Europe/Amsterdam',
                         'Europe/Dublin', 'Europe/Istanbul', 'Europe/Vienna', 'Europe/Zurich']

        coo = 0
        roo = 0
        country_time_zones = [
            pytz.timezone(country_time_zone)
            for country_time_zone in Country_Zones
        ]

        for i in range(len(country_time_zones)):
            if coo == 2:
                roo += 1
                coo = 0

            lbl = Label(timezonelbl, text=str(Country_Zones[i] + " : " + datetime.now(country_time_zones[i]).strftime('%d-%m-%y') + " : " + datetime.now(country_time_zones[i]).strftime('%H:%M:%S')), padx=5, pady=5)
            lbl.grid(row=roo, column=coo, sticky='nw', pady=2)
            coo += 1

        timezonelbl.after(1000, timezones)
    except:
        timezonelbl.after(1000, timezones)

timezones()
root.mainloop()
1 Answers

Don't recreate whole GUI every cycle. The solution is to update only the text values of labels.

from datetime import datetime
from tkinter import Tk, LabelFrame, Label
import pytz

root = Tk()

labels = []

timezonelbl = LabelFrame(root, text="World Time Zones", padx=5, pady=5, highlightbackground="black", highlightthickness=2)
timezonelbl.grid(row=0, column=0, sticky='nsew', pady=2)

# list of desired countries
Country_Zones = ['America/New_York', 'Asia/Kolkata', 'Australia/Sydney', 'Canada/Atlantic', 'Brazil/East',
                    'Chile/EasterIsland', 'Cuba', 'Egypt', 'Europe/Amsterdam',
                    'Europe/Athens', 'Europe/Berlin', 'Europe/Istanbul', 'Europe/Jersey', 'Europe/London',
                    'Europe/Moscow', 'Europe/Paris', 'Europe/Rome', 'Hongkong',
                    'Iceland', 'Indian/Maldives', 'Iran', 'Israel', 'Japan', 'NZ', 'US/Alaska', 'US/Arizona',
                    'US/Central', 'US/East-Indiana', 'Asia/Shanghai',
                    'Europe/Madrid', 'Asia/Urumqi', 'Brazil/Acre', 'Brazil/DeNoronha', 'Brazil/East',
                    'Brazil/West', 'Europe/Amsterdam',
                    'Europe/Dublin', 'Europe/Istanbul', 'Europe/Vienna', 'Europe/Zurich']

coo = 0
roo = 0
country_time_zones = [
    pytz.timezone(country_time_zone)
    for country_time_zone in Country_Zones
]

for i in range(len(country_time_zones)):
    if coo == 2:
        roo += 1
        coo = 0

    lbl = Label(timezonelbl, padx=5, pady=5)
    lbl.grid(row=roo, column=coo, sticky='nw', pady=2)
    labels.append(lbl)
    coo += 1

def update():
    for label, time_zone, country_zone in zip(labels, country_time_zones, Country_Zones):
        label["text"] = str(country_zone + " : " + datetime.now(time_zone).strftime('%d-%m-%y') + " : " + datetime.now(time_zone).strftime('%H:%M:%S'))
    timezonelbl.after(1000, update)
    
update()
root.mainloop()
Related