python - 如何一次按一下而不是按3次按钮?
问题描述
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.button import ButtonBehavior, Button
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
import time
class ImageButton(ButtonBehavior, Image):
pass
class Gui(App):
def build(self):
self.iscapturepressed = False
self.countdown = 3
self.button = ImageButton(source = 'Button.png', size_hint = (None, None), width = 250, height = 250, pos = (300, 300), on_press = self.capture)
self.r3 = Image(source = 'r3.png', size_hint = (None, None), width = 435, height = 783, pos = (0, 0))
self.r2 = Image(source = 'r2.png', size_hint = (None, None), width = 435, height = 783, pos = (0, 0))
self.r1 = Image(source = 'r1.png', size_hint = (None, None), width = 435, height = 783, pos = (0, 0))
self.layout = FloatLayout()
self.layout.add_widget(self.button)
Clock.schedule_interval(self.update, 1 / 30)
return self.layout
def capture(self, *args):
while self.countdown:
if self.countdown == 3:
self.layout.add_widget(self.r3)
print("3")
self.countdown -= 1
return self.capture
elif self.countdown == 2:
self.layout.remove_widget(self.r3)
self.layout.add_widget(self.r2)
print("2")
self.countdown -= 1
return self.capture
elif self.countdown == 1:
self.layout.remove_widget(self.r2)
self.layout.add_widget(self.r1)
print("1")
self.countdown -= 1
return self.capture
self.layout.remove_widget(self.r1)
Clock.schedule_interval(self.update, 1 / 30)
return self.layout
def update(self, *args):
pass
Gui().run()
!!!!!!!!!!!!!!!我是一名新的软件开发人员,在这个 gui 上,我想使用数字的 .png 文件制作倒数计时器(3 到 1)。当我按三下(3 到 1)时,我在 gui 上的按钮会倒计时。但是,当按下一次按钮时,我想要这个倒计时。我需要帮助。!!!!!!!!!!!!!!!!!!
解决方案
我相信问题在于您使用的是 elif 语句,这意味着程序将在每次按下按钮时执行一次。将 elif 语句更改为 if 语句可能会起作用。
def capture(self, *args):
while self.countdown:
if self.countdown == 3:
self.layout.add_widget(self.r3)
print("3")
Clock.schedule_interval(self.update, 1 / 30)
self.countdown -= 1
return self.capture
if self.countdown == 2:
self.layout.remove_widget(self.r3)
self.layout.add_widget(self.r2)
print("2")
Clock.schedule_interval(self.update, 1 / 30)
self.countdown -= 1
return self.capture
if self.countdown == 1:
self.layout.remove_widget(self.r2)
self.layout.add_widget(self.r1)
print("1")
Clock.schedule_interval(self.update, 1 / 30)
self.countdown -= 1
return self.capture
self.layout.remove_widget(self.r1)
return self.layout
def update(self, *args):
pass
Gui().run()
试一试,告诉我它是如何运行的。我只是把它放在一起,没有看到所有东西,我不知道确切的解决方案
推荐阅读
- mysql - 使用子查询显示具有相同值的多个答案
- python-3.x - 如何从另一个类的实例中获取属性?
- ios - 在应用程序中使用自定义框架的 settings.bundle
- angular - '无法使用 Apache thrift 在 Nativescript + Angular 中读取未定义的属性'浏览器''
- java - Java Pong 游戏从桨中弹跳球
- docker - Docker-compose 和 .dockerignore
- postgresql - Postgres:计算时间间隔内的用户操作数
- python - 如何检查是否在 python ContextDecorator 中正确分配了资源?
- html - doc.addImage 不是 Angular 6 中使用的 jspdf 中的函数错误
- airflow - 如何在 Apache Airflow 中重新运行所有失败的任务?