为了实现计时功能,在上一篇基础上再增加一个Label属性来设置计时时间,并添加一个Button来触发事件,将这两个部件放在一个BoxLayout布局中再嵌套进根布局中,新建clock.kv文件,内容具体如下:
<ClockBoxLayout>:
orientation:'vertical'
Label:
id:time_label_id
text:'[b]00[/b]:00:00'
font_size:60
markup:True
BoxLayout:
orientation:'horizontal'
padding:20
spacing:20
size_hint:(1,None)
height:90
Button:
id:start_stop_button_id
text:'Start'
font_size:25
bold:True
border:(2,2,2,2)
on_press:root.start_or_stop()
Label:
id:stopwatch
text:'00:00.[size=40]00[/size]'
font_size:60
markup:True
在main.py文件内,还是通过ID方式获取控件,并改变其text属性,切换按钮显示的文本并修改其状态。具体内容如下:
from time import strftime
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
class ClockBoxLayout(BoxLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)

这篇博客介绍了如何使用Kivy构建一个计时器应用。通过创建ClockBoxLayout类,设置Label显示计时时间,并添加Button触发计时事件。在kv文件中定义布局和控件样式,而在main.py中通过ID获取控件并更新时间显示。当点击'Start'按钮,计时开始,再次点击则暂停计时,按钮文本随之切换。
最低0.47元/天 解锁文章
6016

被折叠的 条评论
为什么被折叠?



