用kivy制作时钟

2022-01-15 00:00:00 python kivy

问题描述

我是 Kivy 的新手,我正在尝试更好地处理事件以及框架的一些基础知识.

I'm new to Kivy, and I'm trying to get a better handle on Events, as well as a few basics of the framework.

为此,谁能提供在 Kivy 中实现的简单时钟的代码,它显示当前时间并每秒更新一次?

For that purpose, can anyone provide the code for a simple clock implemented in Kivy, which shows the current time, and updates every second?


解决方案

这是一个非常简单的时钟:

Here's an extremely simple clock:

from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock

import time

class IncrediblyCrudeClock(Label):
    def update(self, *args):
        self.text = time.asctime()

class TimeApp(App):
    def build(self):
        crudeclock = IncrediblyCrudeClock()
        Clock.schedule_interval(crudeclock.update, 1)
        return crudeclock

if __name__ == "__main__":
    TimeApp().run()

相关文章