the program is a simple watch with different timezones The problem arises when I change the time from one country to the other (pressing the button), the two functions continue to work simultaneously and the schedules overlap
from tkinter import *
from datetime import datetime
import pytz
import time
root = Tk()
root.title("Digital Watch")
root.geometry("450x150")
root.config(bg="#717b85")
#---------------(ARG)Clock function update
def updateARG():
argentina_timezone = pytz.timezone("America/Buenos_Aires")
argentina_hour = datetime.now(argentina_timezone)
hour = argentina_hour.strftime('%H:%M:%S')
date = argentina_hour.strftime('%A %d %B')
country_screen.config(text="Argentina")
watch_screen.config(text=hour)
watch_screen.after(1000,updateARG)
date_screen.config(text=date)
#---------------(MEX)Clock function update
def updateMEX():
mexico_timezone = pytz.timezone("America/Mexico_City")
mexico_hour = datetime.now(mexico_timezone)
hour = mexico_hour.strftime('%H:%M:%S')
date = mexico_hour.strftime('%A %d %B')
country_screen.config(text="MEXICO")
watch_screen.config(text=hour)
watch_screen.after(1000,updateMEX)
date_screen.config(text=date)
#---------------Labels
country_screen = Label(root,text="",font=("Arial Narrow",15), bg="#717b85",fg="white")
country_screen.place(x=10, y= 40)
BotonARG=Button(root, text="ARG",bg="gold",fg="black",font=("Arial
Narrow",15),relief="flat",command=updateARG)
BotonARG.place(x=370,y=0)
BotonMEX=Button(root, text="MEX",bg="gold",fg="black",font=("Arial
Narrow",15),relief="flat",command=updateMEX)
BotonMEX.place(x=370,y=45)
BotonUSA=Button(root, text="USA",bg="gold",fg="black",font=("Arial
Narrow",15),relief="flat")
BotonUSA.place(x=370,y=90)
watch_screen = Label(root,text="",justify="right", fg="#64fff6", bg="#717b85",font=("Arial Narrow",70))
watch_screen.pack()
date_screen = Label(root,text="",justify="right",font=("Arial Narrow",15), bg="#717b85")
date_screen.pack()
root.mainloop()
CodePudding user response:
you can use the method after_cancel : Here you can find how to use it
You just have to call this method before launching after() in your defs
CodePudding user response:
You can try using return and then after that call another function. Return basically stops a specific function. So for example, if you wanted to stop updateMEX(), you would do something like:
def updateMEX():
# Your Code
return
CodePudding user response:
Since the update... functions are almost the same, I suggest leaving one.
from tkinter import *
from datetime import datetime
import pytz
import time
from functools import partial
id_after = None
#---------------Clock function update
def time_config(time_zone, text_timezone):
global id_after
time_hour = datetime.now(time_zone)
hour = time_hour.strftime('%H:%M:%S')
date = time_hour.strftime('%A %d %B')
text = text_timezone.split('/')[1]
country_screen.config(text=text)
watch_screen.config(text=hour)
date_screen.config(text=date)
id_after = watch_screen.after(1000, time_config, time_zone, text_timezone)
def time_zone(text_timezone):
if id_after is not None:
watch_screen.after_cancel(id_after) # Stop timer on change timezone
time_zone = pytz.timezone(text_timezone)
time_config(time_zone, text_timezone)
root = Tk()
root.title("Digital Watch")
root.geometry("450x150")
root.config(bg="#717b85")
country_screen = Label(root,text="",font=("Arial Narrow",15), bg="#717b85",fg="white")
country_screen.place(x=10, y= 40)
BotonARG=Button(root, text="ARG", bg="gold", fg="black", font=("Arial Narrow",15),
relief="flat", command=partial(time_zone, "America/Buenos_Aires"))
BotonARG.place(x=370,y=0)
BotonMEX=Button(root, text="MEX", bg="gold", fg="black", font=("Arial Narrow",15),
relief="flat", command=partial(time_zone, "America/Mexico_City"))
BotonMEX.place(x=370,y=45)
BotonUSA=Button(root, text="USA",bg="gold",fg="black",font=("Arial Narrow",15),relief="flat")
BotonUSA.place(x=370,y=90)
watch_screen = Label(root,text="",justify="right", fg="#64fff6", bg="#717b85",font=("Arial Narrow",70))
watch_screen.pack()
date_screen = Label(root,text="",justify="right",font=("Arial Narrow",15), bg="#717b85")
date_screen.pack()
root.mainloop()