所以我用 python 3.9.5
, kivy 2.0.0rc4
及 kivymd 0.104.2
. 按下“开始”按钮后,开始倒计时。唯一的问题是,当它达到零时,我希望它在60秒时再次重置,因为当玩家回到它时,它会重新开始倒计时。现在我已经设法取消了时间表。它不在零后面。当它达到零后,它停止。但我希望它在60秒时重置,我不知道如何重置。这是我的 python
代码:
import kivy
from kivy.app import App
import kivymd
from kivymd.app import MDApp
import threading
import multiprocessing
import plyer
import ffpyplayer
from plyer import vibrator
from random import randint
from kivy.clock import Clock
from kivymd.uix.button import MDIconButton
from kivymd.uix.textfield import MDTextFieldRound, MDTextField
from kivymd.uix.floatlayout import MDFloatLayout
from kivy.core.audio import SoundLoader
from kivymd.uix.label import MDLabel
from kivy.animation import Animation
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.properties import ObjectProperty, NumericProperty, StringProperty
class Begin(MDFloatLayout):
def start_on(self):
self.ids.to_start.source = 'icons/start_pressed.png'
def start_off(self):
self.ids.to_start.source = 'icons/start.png'
myapp.screen_manager.transition = SlideTransition(direction='left', duration=.25)
myapp.screen_manager.current = 'Beginning1after'
music = SoundLoader.load('sounds/start.wav')
if music:
music.play()
myapp.begin1after.timer_sec(begin1after)
myapp.begin1after.update_label(begin1after)
class Begin1After(MDFloatLayout):
def timer_sec(self, *args):
self.function_interval = Clock.schedule_interval(self.update_label, 1)
Clock.schedule_once(self.stop_interval, 59.5)
def stop_interval(self, *args):
self.function_interval.cancel()
def update_label(self, *args):
self.ids.timeri.text = str(int(self.ids.timeri.text) - 1)
class MyApp(MDApp):
def build(self):
self.screen_manager = ScreenManager()
self.begin1afterdup = Begin1Afterdup()
screen = Screen(name='Begin1Afterdup')
screen.add_widget(self.begin1afterdup)
self.screen_manager.add_widget(screen)
myapp = MyApp()
myapp.run()
这是我的 kivy
代码:
<Begin>:
Button:
text: "start"
background_color: 0, 0, 0, 0
size_hint: .45, .16
pos_hint: {'x': .27, 'y': .3}
on_press: root.start_on()
on_release: root.start_off()
Image:
id: to_start
source: "icons/start.png"
size: 370, 370
allow_stretch: True
allow_ratio: True
keep_ratio: True
center_x: self.parent.center_x
center_y: self.parent.center_y
<Begin1After>:
Label:
id: timeri
text: "60"
pos_hint: {'x': .0, 'y': .4}
font_size: 100
bold: True
color: 1, 0, 0, 1
1条答案
按热度按时间93ze6v8z1#
你为什么不能这样做?
也许,你可能想在进入之前。