如何制作一个倒计时标签,在我按下kivy按钮后开始



我的想法是制作一个倒计时标签,在我按下按钮后开始场景:1.按下按钮2.等一秒钟3.标签更改为14.等一下5.标签更改为26.等一下7.标签更改为3

我一直在尝试

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)

结果:给出错误

代码中的几个问题:

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

因此,使用上面的修改版本的PlayScreen类:

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)

最新更新