【问题标题】:Kivy How can i create time counter with progressbar?Kivy 如何使用进度条创建时间计数器?
【发布时间】:2019-07-11 13:34:18
【问题描述】:

我想用进度条制作时间计数器。进度条应该随着时间的推移而填充。

我已经按照下面代码中的逻辑进行了处理,但是代码在程序打开之前就开始了。

酒吧应该每秒都被塞满。至少我是这么认为的。

'''
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 文件中有与本主题无关的函数。

【问题讨论】:

    标签: kivy


    【解决方案1】:

    Kivy Programming Guide » Events and Properties

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

    解决方案

    解决方案是使用Triggered Events(例如create_trigger()函数)或Schedule Interval(例如schedule_interval()函数)。

    片段 - schedule_interval()

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2020-10-27
      • 2021-10-18
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多