首页 > 解决方案 > 如何一次按一下而不是按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 上的按钮会倒计时。但是,当按下一次按钮时,我想要这个倒计时。我需要帮助。!!!!!!!!!!!!!!!!!!

标签: pythonpython-3.xkivy

解决方案


我相信问题在于您使用的是 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()

试一试,告诉我它是如何运行的。我只是把它放在一起,没有看到所有东西,我不知道确切的解决方案


推荐阅读