繁体   English   中英

如何在我按下按钮 kivy 后开始倒计时 label

[英]how to make a countdown label that start after i press button kivy

My ideas is to make a countdown label that start after i press button Scenario: 1.press button 2.wait one second 3.label change to 1 4.wait one second 5.label change to 2 6.wait one second 7.label更改为 3

我一直在尝试

千伏:

<PlayScreen>:
    name: 'play'
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'shuiitplaymenubg.jpg'
    MDIconButton:
        pos_hint: {'center_x':0.275,'center_y':0.16}
        on_press: root.countdown()
        icon: "startlogo.png"
        user_font_size: 62
    MDIconButton:
        pos_hint: {'center_x':0.275,'center_y':0.06}
        on_press: root.manager.current = 'menu'
        user_font_size: 62
        icon: "backlogo.png"
    Label:
        id: countdown_label
        text: -
        pos_hint: {'center_x':0.82,'center_y':0.585}

派:

class PlayScreen(Screen):
    
    def countdown(self):
        def countdownone(self):
            self.ids.countdown_label.text = "3"
        def countdowntwo(self):
            self.ids.countdown_label.text = "2"  
        def countdownthree(self):
            self.ids.countdown_label.text = "1"
        Clock.schedule_once(countdownone(), 1)
        Clock.schedule_once(countdowntwo(), 2)
        Clock.schedule_once(countdownthree(), 3)

结果:给出错误

您的代码有几个问题:

  1. 通过Clock.schedule方法调用的方法必须接受dt参数(或使用*args )。
  2. Clock.schedule中引用的方法必须是对方法的引用。 使用countdownone()不是对该方法的引用,它实际上执行该方法并尝试安排返回值(即None )。
  3. 您不需要嵌套 function 中的self参数。 外部方法中的self在内部方法中可用。

因此,使用上述内容的PlayScreen class 的修改版本:

class PlayScreen(Screen):

    def countdown(self):
        def countdownone(dt):
            self.ids.countdown_label.text = "3"

        def countdowntwo(dt):
            self.ids.countdown_label.text = "2"

        def countdownthree(dt):
            self.ids.countdown_label.text = "1"

        Clock.schedule_once(countdownone, 1)
        Clock.schedule_once(countdowntwo, 2)
        Clock.schedule_once(countdownthree, 3)

暂无
暂无

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

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