Kivy手势在按钮上注册

2024-05-15 15:53:03 发布

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

我试着用Kivy做一个android应用程序。 对于左右滑动,我使用屏幕管理器和手势,而不是旋转木马。这种方法源于here,我试图使双向刷卡成为可能。你知道吗

当刷卡工作,但是,当按下任何按钮,这也登记为手势,但它不应该。这意味着,大多数时候,当按下按钮,背后的事件之一的手势也被触发。你知道吗

我可以通过增加minscore值来降低发生这种情况的可能性,但这是不可靠的,而且会降低手势的敏感性。你知道吗

有没有办法避免这种情况发生?E、 g.将手势识别仅限于ScreenManager?你知道吗

我附上了这个应用程序的一个例子。你知道吗

非常感谢!你知道吗

主.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import platform
from kivy.uix.screenmanager import NoTransition
import gesture_box as gesture
import time
import datetime


class Storage:
    memory = 0

class Demo(gesture.GestureBox):

    def init(self):
        today = time.localtime().tm_wday # 0 is Monday
        l = ['one', 'two', 'three', 'four', 'five', 'six', 'six']
        Storage.memory = today
        return l[today]

    def screen_up(self):
        print Storage.memory

        l = ['two', 'three', 'four', 'five', 'six', 'six']
        try:
            for i in range(6):
                if Storage.memory == 5:
                    return l[Storage.memory]
                if Storage.memory == i:
                    Storage.memory = i + 1
                    return l[i]
        except:
            pass

    def screen_down(self):
        print Storage.memory

        l = ['one', 'one', 'two', 'three', 'four', 'five']
        try:
            for i in range(5, -1, -1):
                if Storage.memory == 0:
                    return l[Storage.memory]
                if Storage.memory == i:
                    Storage.memory = i - 1
                    return l[i]
        except:
            pass

    def do_something_1(self):
        print 'Something 1 was done.'

        self.display_1 = 'Something 1 on Monday'
        self.display_2 = 'Something 1 on Tuesday'
        self.display_3 = 'Something 1 on Wednesday'
        self.display_4 = 'Something 1 on Thursday'
        self.display_5 = 'Something 1 on Friday'
        self.display_6 = 'Something 1 on Weekend'

    def do_something_2(self):
        print 'Something 2 was done.'

        self.display_1 = 'Something 2 on Monday'
        self.display_2 = 'Something 2 on Tuesday'
        self.display_3 = 'Something 2 on Wednesday'
        self.display_4 = 'Something 2 on Thursday'
        self.display_5 = 'Something 2 on Friday'
        self.display_6 = 'Something 2 on Weekend'


class MainApp(App):
    def build(self):
        return Demo()

    def on_pause(self):
        # Here you can save data if needed
        return True

    def on_resume(self):
        # Here you can check if any data needs replacing (usually nothing)
        pass

if __name__ == '__main__':
    app = MainApp()
    app.run()

手势_方框.py

from kivy.gesture import GestureDatabase
from kivy.uix.boxlayout import BoxLayout
from kivy.gesture import Gesture 

gesture_strings = {
    'left_to_right_line': 'eNqNWNtS3UYQfNePmJdQc5/dHyCvqeIDUsScAsoOnILjJP77jGYFPntsScsLptVq7XSvZke+evry9M/364fD2+nb62H6ffl9hOnq/ojT7afnu78Pn6YjxT/jF09vt5/eTq8vXw5v8adMV1+POl39UuQ2adPRZimP+48vT8+n+bYy31ZXbvtjZk1HbCuYl/A9bkGabn6DawApWioLWiWvAjov6L+ZwEkwJhckUFSptdD09tfd9nMkn6PTw8YjHt7VASoxEDNbQZN99SwefUhdDZUqFK5CxX1fvKR4HROn6ggVCEms6r4xlAEQDqnHFXdUKFiUIR6xL08pz2PyTsiKIirBcN33hjJWGotVBAsTopBoBed99YyVxmIVPI/Vqu3LZ7B0HiwWKsaIXKqbmJ3ps1WMxwsqBiUs2tXnjJZxUJ/7bPflM1rmQXkU1crFqjMWgH33ObNlHdMnZ1OgamACpeK+fIbLPijPpcR1REI1EBpYfqbLg+kSGrgoFymFw6v9rS+Zrgymi65oKhIvVaXYtwP6Ga8MxhvLB+TKJORO8aB9/YxXBuNFMiugtUIVrrQfr2S8Mhgv1K417G9+yXRlMF2Q6BVePChoFi/xrr5mujqYLly8u2VfP9PVkXTna2xEwnHgBqOQ6r5+pqs6pk/RGAAgmqbFCVD31TNc9TF1Byqxb8L/MEcGvMlstY6p1zDc2MGAIgTb35mW0RoOySPGiBBHirEBKvP+e2uZrI0lG83MNFpnNLRSY6Da3/iWwdpYsLEdRcAwZikrBDAgn8naWLLRi13YhJlq0YEjxTJZW5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guxU6zxV1698E1+Ap1G8i6C9xcdE4AZ8kLJABJUAAhEk6DGBCBJ5gQgS5QIROGhDgQgcSCkQgaJ0wftwMwt4j5pZQHUKylOnWqJmVgIkWKvqVEs0ldWplhIsLFVAAsHUqZYcqKEZkKhop1qiqawVkMiLsyvRPDWwK0HRBnYlyMXAbjRyDexGjWxgl6NAYDeaygZ2KfICdlMw/AzspiiQs8staIbs7HINFlV2drkE/ZIZkIC67OxyjnJxdtmCorMBEsiYnV3WKF1nlyUKVAGJ0gW70RZdwG7UDAXsRlOtgF0Kii5gN+n2s3IT71RTCxgoBkgU1ammqHlKASRiwKnubwGkARJoirsYlSBd3My6tAGEAYkCCSBBZ+AOR9H4xI2uj4IAAnajCYvbHi03tepXQMINj5bT8GIFh8u6aLDiJkfLmXexgqnl8LBYQc4yrBcr+FDaWkGBpq0VVS8njsWKQuUqB9QmV9FQm1z5RW288cu4UBHb1oramLdW1MZpa0VtdOUXtdHVaa2tISiUNICg6nlVr1+0hoACSgEEfKSrDMFH2ujHuAJRoq3V+VgmwWJ0OtrWK242LW+NTkbbcozbS7sK7wW3Le+4odSr6F5Uzev+ZVw8luPR2YirRpWt0SuqtDV6RZejE4xe0WXywOgVXWYNjF7R9gsYxrXgctaCsVeEb5y+HZ6fvp3mn7T7+f5h5rIb/3n+evrmtt4M+OaiW0+vfx7eHl+++A/g/eg+/1Yy289fh/1+fHv9+uOL++qn9ge+r4VL5Vb7Kk9J569H7/8Dxj5mIw=='
        }

#This database can compare gestures the user makes to its stored     gestures 
#and tell us if the user input matches any of them.
gestures = GestureDatabase()
for name, gesture_string in gesture_strings.items():
    gesture = gestures.str_to_gesture(gesture_string)
    gesture.name = name
    gestures.add_gesture(gesture)

class GestureBox(BoxLayout):

    def __init__(self, **kwargs):
        for name in gesture_strings:
            self.register_event_type('on_{}'.format(name))
        super(GestureBox, self).__init__(**kwargs)

    def on_left_to_right_line(self):
        pass

    def on_right_to_left_line(self):
        pass

#To recognize a gesture, you will need to start recording each individual event in the
#touch_down handler, add the data points for each call to touch_move , and then do the
#gesture calculations when all data points have been received in the touch_up handler.

    def on_touch_down(self, touch):
        #create an user defined variable and add the touch coordinates 
        touch.ud['gesture_path'] = [(touch.x, touch.y)]    
        super(GestureBox, self).on_touch_down(touch)

    def on_touch_move(self, touch):
        touch.ud['gesture_path'].append((touch.x, touch.y))
        super(GestureBox, self).on_touch_move(touch)

    def on_touch_up(self, touch):
        if 'gesture_path' in touch.ud:
            #create a gesture object
            gesture = Gesture()    
            #add the movement coordinates 
            gesture.add_stroke(touch.ud['gesture_path'])
            #normalize so thwu willtolerate size variations
            gesture.normalize()
            #minscore to be attained for a match to be true, default 0.3
            match = gestures.find(gesture, minscore=0.3)
            if match:
                print "{} happened".format(match[1].name)
                self.dispatch('on_{}'.format(match[1].name))
        super(GestureBox, self).on_touch_up(touch)

主电源.kv

#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
<Demo>:
    on_parent: manager.transition = NoTransition(); manager.current = root.init(); manager.transition = SlideTransition()
    #Handling the gesture event.
    on_left_to_right_line: manager.current = root.screen_down();  manager.transition.direction = 'right'
    on_right_to_left_line: manager.current = root.screen_up();  manager.transition.direction = 'left'

    display_1: 'Monday'
    display_2: 'Tuesday'
    display_3: 'Wednesday'
    display_4: 'Thursday'
    display_5: 'Friday'
    display_6: 'Weekend'

    BoxLayout:
        orientation: 'vertical'

        ScreenManager:
            size_hint_y: 80
            id: manager
            Screen:
                name: "one"

                BoxLayout:
                    orientation: 'vertical'
                    Label:
                        text: root.display_1
                        size_hint_y: 80
                        halign: 'center'
                        text_size: (800, None)
                        color: (1, 1, 0, 1)

            Screen:
                name: "two"

                BoxLayout:
                    orientation: 'vertical'
                    Label:
                        text: root.display_2
                        size_hint_y: 80
                        halign: 'center'
                        text_size: (800, None)
                        color: (1, 1, 0, 1)

            Screen:
                name: "three"

                BoxLayout:
                    orientation: 'vertical'
                    Label:
                        text: root.display_3
                        size_hint_y: 80
                        halign: 'center'
                        text_size: (800, None)
                        color: (1, 1, 0, 1)

            Screen:
                name: "four"

                BoxLayout:
                    orientation: 'vertical'
                    Label:
                        text: root.display_4
                        size_hint_y: 80
                        halign: 'center'
                        text_size: (800, None)
                        color: (1, 1, 0, 1)

            Screen:
                name: "five"

                BoxLayout:
                    orientation: 'vertical'
                    Label:
                        text: root.display_5
                        size_hint_y: 80
                        halign: 'center'
                        text_size: (800, None)
                        color: (1, 1, 0, 1)

            Screen:
                name: "six"

                BoxLayout:
                    orientation: 'vertical'
                    Label:
                        text: root.display_6
                        size_hint_y: 80
                        halign: 'center'
                        text_size: (800, None)
                        color: (1, 1, 0, 1)

        Button:
            size_hint_y: 10
            text: 'Do something 1'
            background_color: (0.5, 0.5, 0.5, 1.0)
            on_release: root.do_something_1()

        Button:
            size_hint_y: 10
            text: 'Do something 2'
            background_color: (0.5, 0.5, 0.5, 1.0)
            on_release: root.do_something_2()

Tags: totextnameimportselfsizeondef