Python 如何多次运行函数,但只运行一次指令列表?

Python 如何多次运行函数,但只运行一次指令列表?,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy Language,我制作了一个有几个按钮的程序,我希望当我按下按钮几次时,指令列表只启动一次 我试着用一个全局变量,但当我想在两个屏幕上使用相同的变量时,我不能这样做 我知道我可以使用两个全局变量,但我想在两个屏幕中使用相同的一个 当我们进入第二个屏幕时,u如何取值为“True”?或者有一个更简单的解决方案与装饰 这是我的密码: import kivy from kivy.app import App from kivy.uix.button import Button from kivy.uix.floatla

我制作了一个有几个按钮的程序,我希望当我按下按钮几次时,指令列表只启动一次

我试着用一个全局变量,但当我想在两个屏幕上使用相同的变量时,我不能这样做

我知道我可以使用两个全局变量,但我想在两个屏幕中使用相同的一个

当我们进入第二个屏幕时,u如何取值为“True”?或者有一个更简单的解决方案与装饰

这是我的密码:

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()

导入kivy
从kivy.app导入应用程序
从kivy.uix.button导入按钮
从kivy.uix.floatlayout导入floatlayout
从kivy.uix.label导入标签
从kivy.core.window导入窗口
从kivy.lang导入生成器
从kivy.uix.screenmanager导入screenmanager,屏幕
随机输入
从kivy.properties导入ObjectProperty
从kivy.properties导入ListProperty、StringProperty
kivy.uix.screenmanager.FadeTransition
kv=”“”
#:导入NotTransition kivy.uix.screenmanager.NotTransition
#:导入Clock kivy.Clock.Clock
#:导入FadeTransition kivy.uix.screenmanager.FadeTransition
#:导入RiseInTransition kivy.uix.screenmanager.RiseInTransition
#:导入CardTransition kivy.uix.screenmanager.CardTransition
MyScreenManager:
转换:RiseInTransition()
问题1:
姓名:“问题1”
问题2:
姓名:“问题2”
:
标签宽度:比率
浮动布局:
按钮:
案文:“+1”
位置:270300
大小提示:.30,.10
背景颜色:0,1,0,0.75
新闻界:
root.mauvais()
按钮:
案文:“以下”
位置:270240
大小提示:.30,.10
背景颜色:0,1,0,0.75
新闻界:
时钟。时间表_一次(root.suite,0.75)
标签:
id:比率
text:root.manager.theText
位置:280270
字体大小:17
颜色:0,0,1,0.65
:
标签2:比率
浮动布局:
按钮:
案文:“+1”
位置:270300
大小提示:.30,.10
背景颜色:0,1,0,0.75
新闻界:
root.mauvais()
标签:
id:比率
text:root.manager.theText
位置:280270
字体大小:17
颜色:0,0,1,0.65
"""
t=0
m=真
类MyScreenManager(屏幕管理器):
theText=StringProperty(“”)
课堂问题1(屏幕):
m=真
def mauvais(自我):
全球m
如果m==真:
全球t
t=t+1
self.manager.theText=str(t)
m=假
def套件(根目录,文本):
root.manager.current=“问题2”
通过
课堂问题2(屏幕):
全球m
m=真
def mauvais(自我):
全球m
如果m==真:
全球t
t=t+1
self.manager.theText=t
m=假
通过
课堂测验(应用程序):
def生成(自):
self.title='Quizz'
Window.clearcolor=(0,1,1,0.25)
返回生成器。加载字符串(kv)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
Quizz().run()

因此,由于我没有得到我的问题的答案,我提出了一个解决方案,可以同时解决两种可能性:

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()
导入kivy
从kivy.app导入应用程序
从kivy.clock导入时钟
从kivy.uix.button导入按钮
从kivy.core.window导入窗口
从kivy.lang导入生成器
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.properties导入StringProperty、BooleanProperty、NumericProperty
kivy.uix.screenmanager.FadeTransition
kv=”“”
#:导入NotTransition kivy.uix.screenmanager.NotTransition
#:导入Clock kivy.Clock.Clock
#:导入FadeTransition kivy.uix.screenmanager.FadeTransition
#:导入RiseInTransition kivy.uix.screenmanager.RiseInTransition
#:导入CardTransition kivy.uix.screenmanager.CardTransition
MyScreenManager:
转换:RiseInTransition()
问题1:
姓名:“问题1”
问题2:
姓名:“问题2”
:
标签宽度:比率
浮动布局:
按钮:
案文:“+1”
位置:270300
大小提示:.30,.10
背景颜色:0,1,0,0.75
新闻界:
root.manager.mauvais()方法现在位于MyScreenManager中
按钮:
案文:“以下”
位置:270240
大小提示:.30,.10
背景颜色:0,1,0,0.75
新闻界:
时钟。时间表_一次(root.suite,0.75)
标签:
id:比率
text:root.manager.theText
位置:280270
字体大小:17
颜色:0,0,1,0.65
:
标签2:比率
浮动布局:
按钮:
案文:“+1”
位置:270300
大小提示:.30,.10
背景颜色:0,1,0,0.75
新闻界:
root.manager.mauvais()方法现在位于MyScreenManager中
标签:
id:比率
text:root.manager.theText
位置:280270
字体大小:17
颜色:0,0,1,0.65
"""
#是时候忽略额外的点击了
#如果该值为零,则忽略所有其他单击
单击忽略时间=3
类MyScreenManager(屏幕管理器):
theText=StringProperty(“”)
m=布尔属性(真)
t=数值属性(0)
def mauvais(自我):
如果self.m==True:
self.t+=1
self.theText=str(self.t)
self.m=False
如果单击忽略时间>0:
时钟。计划一次(self.reset\u m,单击忽略时间)
其他:
打印('忽略单击')
def复位(自身,dt):
打印('reset m')
self.m=True
课堂问题1(屏幕):
def套件(根目录,文本):
root.manager.current=“问题2”
课堂问题2(屏幕):
通过
课堂测验(应用程序):
def