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

2024-09-29 02:19:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想生成一个计数,如果某个事件发生而我没有得到它,它会将计数器加1。其思想是,当将其中一个正确的图像(Dropable_zone_Object:[to_box,])拖放到正确的位置时,计数器应添加+1。目前,该应用程序工作正常,但在打印计数器时,其值始终为1。我应该换什么

App screenshot

Arrastar.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

阿拉斯特拉比

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

Tags: tofromposselfboxzonesourceimg
3条回答

我还是无法进入下一个屏幕,约翰。我已经在ScreenManager中设置了计数器和If,但是我仍然无法从一个页面转到另一个页面,关于ScreenManager的操作有些我不知道

Arrastar.kv

#:kivy 1.9.0
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<RelScreenManager>:
    transition: FadeTransition()
    Relaciona3x2:
    Relaciona4x2:
    Relaciona5x2:

<Relaciona3x2>:
    name: 'Rel3x2'
    id: Rel3x2
    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>:
    name: 'Rel4x2'
    id: Rel4x2
    AnchorLayout:
        Image:
            source: "data/img/fondobosque.jpg"
            allow_stretch: True
            keep_ratio: False
            
<Relaciona5x2>:
    name: 'Rel5x2'
    id: Rel5x2
    AnchorLayout:
        Image:
            source: "data/img/fondobosque.jpg"
            allow_stretch: True
            keep_ratio: False

阿拉斯特拉比

from kivy.uix.boxlayout import BoxLayout
__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
from kivy.uix.widget import Widget

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 RelScreenManager(ScreenManager):
    def __init__(self):
        super(RelScreenManager, self).__init__()
        
        self.cuenta = Contador()
        if self.cuenta.numero == 3:
            self.screen_manager.current = "Rel4x2·"

#
#    def Screen3x2(self):
#        self.screen_manager.current = "Rel3x2"
#        
#   def Screen4x2(self):
#       self.screen_manager.current = "Rel4x2"
#       
#   def Screen5x3(self):
#       self.screen_manager.current = "Rel5x2"
        
class Relaciona3x2(Screen):
    pass

class Relaciona4x2(Screen):
    pass

class Relaciona5x2(Screen):
    pass

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

谢谢John,计数器已经做了正确的求和,但是现在当我尝试在计数器达到3时转到下一个屏幕时,它什么也不做

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

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

相关问题 更多 >