当我打电话给另一个功能时,如何停止功能?使用tkinter python
该程序是一款具有不同时区的简单手表 当我将时间从一个国家更改为另一个国家(按按钮)时,出现了问题,这两个功能继续同时工作,时间表重叠
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用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
由于更新...功能几乎相同,我建议留下一个。
Since the update... functions are almost the same, I suggest leaving one.