简体   繁体   中英

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 change to 3

What Ive been try

kv:

<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}

Py:

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)

Result: Giving error

Several problems with your code:

  1. Methods called via the Clock.schedule methods must accept a dt argument (or use *args ).
  2. Methods referenced in the Clock.schedule must be references to a method. Using countdownone() is not a reference to that method, it actually executes that method and tries to schedule the return value (which is None ).
  3. You do not need the self argument in a nested function. The self from the outer method is available in the inner method.

So a modified version of your PlayScreen class using the above:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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