当我打电话给另一个功能时,如何停止功能?使用tkinter python

发布于 2025-02-11 09:28:54 字数 1904 浏览 5 评论 0原文

该程序是一款具有不同时区的简单手表 当我将时间从一个国家更改为另一个国家(按按钮)时,出现了问题,这两个功能继续同时工作,时间表重叠

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有深☉意 2025-02-18 09:28:54

您可以使用after_cancel的方法:
在这里您可以找到如何使用它

您只需要在def中启动()之后启动之前调用此方法

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

伊面 2025-02-18 09:28:54

由于更新...功能几乎相同,我建议留下一个。

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文