简体   繁体   English

如何在 tkinter 上更新时钟界面?

[英]how do i make the clock interface update on tkinter?

I'm trying to build a clock with python and tkinter but I don't know how to make the tkinter UI update every second.我正在尝试使用 python 和 tkinter 构建一个时钟,但我不知道如何使 tkinter UI 每秒更新一次。 Right now it shows the time on the UI but it doesn't updtade.现在它在 UI 上显示时间,但不会更新。 I have tried to make a loop with.after but python gives the following error:我试图用 .after 做一个循环,但 python 给出了以下错误:

RecursionError: maximum recursion depth exceeded while calling a Python object. RecursionError: maximum recursion depth exceeded while calling a Python object。

How do I fix this?我该如何解决?

Code:代码:

import datetime
from tkinter import *
from tkinter.ttk import *

class clock:
    def __init__(self):
        pass

        
    def get_hour(self):
        self.date_time = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S/%p")
        self.date, self.time1 = self.date_time.split()
        self.time2, self.time3 = self.time1.split('/')
        self.hour, self.minutes, self.seconds =  self.time2.split(':')
    
    def format_hour(self):
        if int(self.hour) > 12 and int(self.hour) < 24:
                self.time = str(int(self.hour) - 12) + ':' + self.minutes + ':' + self.seconds + ' ' + self.time3
                return self.time
        else:
                self.time = self.time2 + ' ' + self.time3
                return self.time
                
                
    def execute(self):
        while True:
            self.get_hour()
            return self.format_hour()
            
class clock_ui:
    def __init__(self, the_window):
        self.the_window = the_window
        self.the_window.title("Clock")
        self.the_window.geometry('1300x550')
        self.tabs()
        self.update_ui()
        

# Adds the tabs for the user to select on the interface:
    def tabs(self):
        self.tabs_control = Notebook(self.the_window)
        self.clock_tab = Frame(self.tabs_control)
        self.alarm_tab = Frame(self.tabs_control)
        self.stopwatch_tab = Frame(self.tabs_control)
        self.timer_tab = Frame(self.tabs_control)
        self.tabs_control.add(self.clock_tab, text="Clock")
        self.tabs_control.add(self.alarm_tab, text="Alarm")
        self.tabs_control.add(self.stopwatch_tab, text='Stopwatch')
        self.tabs_control.add(self.timer_tab, text='Timer')
        self.tabs_control.pack(expand = 1, fill ="both")

        # Adds the looks on the clock tab:
        self.time_label = Label(self.clock_tab, font = 'calibri 40 bold', foreground = 'black')
        self.time_label.pack(anchor='center')
        self.date_label = Label(self.clock_tab, font = 'calibri 40 bold', foreground = 'black')
        self.date_label.pack(anchor='s')
        
    def update_ui(self):
        self.time_label.config(text = clock().execute())
        # self.date_label.config(text= date)
        # self.time_label.after(1000, self.update_ui())
        
app = Tk()
my_clock = clock_ui(app)
app.mainloop()

In line 59, you must declared variable.在第 59 行,您必须声明变量。 And line 60, then add string to self.date_label.config .第 60 行,然后将string添加到self.date_label.config

def update_ui(self):
    string = self.time_label.config(text = clock().execute())
    self.date_label.config(text=string)
    self.time_label.after(1000, self.update_ui)

You will see second second pass continuously.您会连续看到第二次第二次通过。

Result output:结果 output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM