Search code examples
pythonkivycounter

How to increment a counter when an event occurs on kivy


I want to generate a count that adds 1 to the counter if a certain event happens and I am not getting it. The idea is that when one of the correct images (droppable_zone_objects: [to_box, ]) is dragged and dropped to the correct location, the counter should add +1. Right now the app works fine but when printing the counter, its value is always 1. What should I change?

App screenshot

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

Solution

  • You are creating a new Contador instance in every call to greet(). You should probably create just one instance of Contador, like this:

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