当事件发生在 kivy 上时如何增加计数器

How to increment a counter when an event occurs on kivy

我想生成一个计数,如果某个事件发生但我没有得到它,则计数器加 1。这个想法是,当其中一张正确的图像 (droppable_zone_objects: [to_box, ]) 被拖放到正确的位置时,计数器应该加 +1。现在应用程序运行正常,但在打印计数器时,它的值始终为 1。我应该更改什么?

arrastrar.kv

#:kivy 1.9.0

<Relaciona3x2>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'data/img/fondobosque.jpg'
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            id: from_box
            
            Widget:
            
            DragableButton:
                canvas:
                    Rectangle:
                        pos: self.pos
                        size: self.size
                        source: 'data/img/letra_i/iglu.png'
                bound_zone_objects: [from_box, to_box ]
                droppable_zone_objects: [to_box, ]
                drop_func: app.greet
                
            Widget:
                
            DragableButton:
                canvas:
                    Rectangle:
                        pos: self.pos
                        size: self.size
                        source: 'data/img/letra_i/indio.png'
                bound_zone_objects: [from_box, to_box, ]
                droppable_zone_objects: [to_box, ]
                drop_func: app.greet

            Widget:
                
            DragableButton:
                canvas:
                    Rectangle:
                        pos: self.pos
                        size: self.size
                        source: 'data/img/letra_m/moto.png'
                bound_zone_objects: [from_box, to_box, ]
                #droppable_zone_objects: [to_box, ]
                drop_func: app.greet
                
            Widget:
        
        BoxLayout:
            id: from_box
            
            Widget:
            
            DragableButton:
                canvas:
                    Rectangle:
                        pos: self.pos
                        size: self.size
                        source: 'data/img/letra_u/una.png'
                bound_zone_objects: [from_box, to_box, ]
                #droppable_zone_objects: [to_box, ]
                drop_func: app.greet
                
            Widget:
            
            DragableButton:
                canvas:
                    Rectangle:
                        pos: self.pos
                        size: self.size
                        source: 'data/img/letra_i/iman_1.png'
                bound_zone_objects: [from_box, to_box, ]
                droppable_zone_objects: [to_box, ]
                drop_func: app.greet
                
            Widget:
            
            DragableButton:
                canvas:
                    Rectangle:
                        pos: self.pos
                        size: self.size
                        source: 'data/img/letra_u/urraca.png'
                bound_zone_objects: [from_box, to_box, ]
                #droppable_zone_objects: [to_box, ]
                drop_func: app.greet

            Widget:
            
        Image:
            id: to_box
            source: "data/img/caldero.png"
            
<Relaciona4x2>:
    AnchorLayout:
        Image:
            source: "data/img/fondobosque.jpg"
            allow_stretch: True
            keep_ratio: False
            
<Relaciona5x2>:
    AnchorLayout:
        Image:
            source: "data/img/fondobosque.jpg"
            allow_stretch: True
            keep_ratio: False

arrastrar.py

__all__ = ('Relaciona3x2', 'Relaciona4x2', 'Relaciona5x2')

import kivy
kivy.require('1.0.6')

from DragableButton import DragableButton
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window

Builder.load_file('arrastrar.kv')

class Contador(object):
    def __init__(self, inicial=0):
        self.numero = inicial
    
    def next(self):
        self.numero += 1
        return self.numero

class Relaciona3x2(Screen):
    pass

class Relaciona4x2(Screen):
    pass

class Relaciona5x2(Screen):
    pass

class ArrastraApp(App):
    def build(self):
        #Window.fullscreen = 'auto'
        return Relaciona3x2()
    
    def greet(self):
        cuenta = Contador()
        print('Draggin done!')
        if DragableButton.droppable_zone_objects is True:
            print(cuenta.next())
        
if __name__ == "__main__":
    ArrastraApp().run()

您在每次调用 greet() 时都会创建一个新的 Contador 实例。您应该只创建一个 Contador 实例,如下所示:

class ArrastraApp(App):
    def build(self):
        #Window.fullscreen = 'auto'
        self.cuenta = Contador()
        return Relaciona3x2()
    
    def greet(self):
        # cuenta = Contador()
        print('Draggin done!')
        if DragableButton.droppable_zone_objects is True:
            print(self.cuenta.next())