Kivy如何使用进度条创建计时器? [英] Kivy How can i create time counter with progressbar?

查看:118
本文介绍了Kivy如何使用进度条创建计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让进度条与时间相反.随着时间的推移,该栏应被填充.

I want to make time counter with progressbar.The bar should be filled as time progresses.

我已经理解了以下代码中的逻辑,但是代码在程序打开之前就开始了.

I've progresed with the logic in the codes below, but the code starts before the program opens.

酒吧应该每秒钟塞满东西.至少我是这样认为的.

The bar should be stuffed every second.At least that's what I think.

'''
def update_time(self):
    while self.ids.pb.value < 30:
        time.sleep(1)
        self.ids.pb.value+=1
'''

相关的.kv文件.

'''
<Question>:
    name:"questions"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'bg2.jpg'
    FloatLayout:

        Label:
            id:quest
            pos_hint: {"x":0.1,"y":0.62}
            size_hint: 0.7,0.5
            text:root.sendquest()
            color:1, 0, 0, 1

        ProgressBar:
            id : pb
            min :0
            max :30
            pos_hint: {'x': 0.1,"y":0.45}
            size_hint_x :0.8
            size_hint_y :0.03
            background_color:0.8, 0.1, 0.1, 0
        Button: #A
            id:A
            pos_hint: {"x":0.045,"y":0.376}
            size_hint: 0.91,0.055
            on_press:
                root.reset() if root.check_truth("A") else root.popup()
'''

main.py文件中的某些功能与此主题无关​​.

There are functions that are not relevant to this subject in main.py file.

推荐答案

Kivy编程指南»活动和属性

在Kivy应用程序中,您必须避免长/无限循环或 睡觉.

In Kivy applications, you have to avoid long/infinite loops or sleeping.

解决方案

解决方案是使用触发事件(例如 create_trigger() 函数)或计划间隔(例如

Solution

The solution is to use either Triggered Events (e.g. create_trigger() function) or Schedule Interval (e.g. schedule_interval() function).

from kivy.properties import ObjectProperty
from kivy.clock import Clock


class RootWidget(ProgressBar):
    tick = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.max = 30
        self.tick = Clock.schedule_interval(lambda dt: self.update_time(), 1)

    def update_time(self):
        self.value += 1
        if self.value >= 30:
            self.tick.cancel()    # cancel event

摘要-create_trigger()

from kivy.properties import ObjectProperty
from kivy.clock import Clock


class RootWidget(ProgressBar):
    tick = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.max = 30
        self.tick = Clock.create_trigger(lambda dt: self.update_time(), 1)
        self.tick()

    def update_time(self):
        self.value += 1
        if self.value < 30:
            self.tick()
        else:
            self.tick.cancel()    # cancel event

这篇关于Kivy如何使用进度条创建计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆