简体   繁体   English

如何多次运行 function 但只运行一次指令列表?

[英]How to run a function several times but run the instruction list only once?

I made a program with several buttons and I wish that when I press the button several times, the list of instructions only launches once.我制作了一个带有几个按钮的程序,我希望当我多次按下按钮时,指令列表只启动一次。

I tried to do it with a global variable but when I want to use the same variable for both screens, I can't do it.我试图用一个全局变量来做,但是当我想为两个屏幕使用相同的变量时,我做不到。

I know I can do it using two global variables but I would like to use the same one in both screens.我知道我可以使用两个全局变量来做到这一点,但我想在两个屏幕上都使用同一个。

How, when we get to the second screen, u takes the value "True"?当我们到达第二个屏幕时,如何获取值“True”? Or is there a simpler solution with a decorator?或者有没有更简单的装饰器解决方案?

Here is my code:这是我的代码:

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import random
from kivy.properties import ObjectProperty
from kivy.properties import ListProperty, StringProperty
kivy.uix.screenmanager.FadeTransition


kv = """

#: import NoTransition kivy.uix.screenmanager.NoTransition     
#:import Clock kivy.clock.Clock
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import RiseInTransition kivy.uix.screenmanager.RiseInTransition
#: import CardTransition kivy.uix.screenmanager.CardTransition

MyScreenManager:
    transition: RiseInTransition()
    Question1:
        name: "question1"
    Question2:
        name: "question2"


<Question1>:

    label_wid : ratio

    FloatLayout:

        Button:
            text: "+1"
            pos: 270, 300
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press:
                root.mauvais()
        Button:
            text: "following"
            pos: 270, 240
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press: 
                Clock.schedule_once(root.suite, 0.75)

        Label:
            id: ratio
            text: root.manager.theText 
            pos: 280,270
            font_size: 17
            color: 0,0,1,0.65

<Question2>:
    label_wid2: ratio
    FloatLayout:
        Button:
            text: "+1"
            pos: 270, 300
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press:
                root.mauvais()

        Label:
            id: ratio
            text: root.manager.theText
            pos: 280,270
            font_size: 17
            color: 0,0,1,0.65


"""

t=0 
m=True



class MyScreenManager(ScreenManager):
    theText = StringProperty('')  


class Question1(Screen):

    m= True
    def mauvais(self):
        global m
        if  m==True:
            global t
            t=t+1
            self.manager.theText = str(t)
            m=False        

    def suite(root,text):
        root.manager.current = "question2"

    pass 

class Question2(Screen):    

    global m
    m= True
    def mauvais(self):
        global m
        if  m==True:
            global t
            t=t+1
            self.manager.theText = t
            m=False     
    pass



class Quizz(App):
    def build(self):
        self.title = 'Quizz'
        Window.clearcolor = (0, 1, 1, 0.25)

        return Builder.load_string(kv)


if __name__ == '__main__':
    Quizz().run()

So, since I didn't get an answer to my question, I have a proposed solution that does both possibilities:所以,由于我没有得到我的问题的答案,我有一个建议的解决方案,它可以实现两种可能性:

import kivy
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty, BooleanProperty, NumericProperty
kivy.uix.screenmanager.FadeTransition


kv = """

#: import NoTransition kivy.uix.screenmanager.NoTransition     
#:import Clock kivy.clock.Clock
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import RiseInTransition kivy.uix.screenmanager.RiseInTransition
#: import CardTransition kivy.uix.screenmanager.CardTransition

MyScreenManager:
    transition: RiseInTransition()
    Question1:
        name: "question1"
    Question2:
        name: "question2"


<Question1>:

    label_wid : ratio

    FloatLayout:

        Button:
            text: "+1"
            pos: 270, 300
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press:
                root.manager.mauvais()  # method is now in MyScreenManager
        Button:
            text: "following"
            pos: 270, 240
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press: 
                Clock.schedule_once(root.suite, 0.75)

        Label:
            id: ratio
            text: root.manager.theText
            pos: 280,270
            font_size: 17
            color: 0,0,1,0.65

<Question2>:
    label_wid2: ratio
    FloatLayout:
        Button:
            text: "+1"
            pos: 270, 300
            size_hint: .30, .10
            background_color: 0,1,0,0.75
            on_press:
                root.manager.mauvais()  # method is now in MyScreenManager

        Label:
            id: ratio
            text: root.manager.theText
            pos: 280,270
            font_size: 17
            color: 0,0,1,0.65
"""

#  time to ignore additional clicks
#  if this is zero, ignore all additional clicks
CLICK_IGNORE_TIME = 3

class MyScreenManager(ScreenManager):
    theText = StringProperty('')
    m = BooleanProperty(True)
    t = NumericProperty(0)

    def mauvais(self):
        if  self.m==True:
            self.t += 1
            self.theText = str(self.t)
            self.m=False
            if CLICK_IGNORE_TIME > 0:
                Clock.schedule_once(self.reset_m, CLICK_IGNORE_TIME)
        else:
            print('ignored click')

    def reset_m(self, dt):
        print('reset m')
        self.m = True


class Question1(Screen):
    def suite(root,text):
        root.manager.current = "question2"


class Question2(Screen):
    pass


class Quizz(App):
    def build(self):
        self.title = 'Quizz'
        Window.clearcolor = (0, 1, 1, 0.25)

        return Builder.load_string(kv)


if __name__ == '__main__':
    Quizz().run()

This code moves the mauvais() method to the MyScreenManager class, since both versions were identical.此代码将mauvais()方法移动到MyScreenManager class,因为两个版本是相同的。

The CLICK_IGNORE_TIME is how long to ignore additional clicks. CLICK_IGNORE_TIME是忽略额外点击的时间。 If it is zero all additional clicks will be ignored如果为零,则所有额外的点击都将被忽略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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