ScrollView和触摸事件不能在kivy中一起工作

时间:2017-07-18 04:26:59

标签: python kivy

我有一个屏幕,其中我有可滚动的boxlayout。它工作正常,我可以滚动屏幕内的标签,文本输入和按钮

 #kvfile
<FirstScreen>:
    ScrollView:
        BoxLayout:
            orientation:"vertical"
            size_hint_y: None 
            height: self.minimum_height  
            #here some scrollable labels, text inputs, and buttons

#python file
class FirstScreen(Screen):
    '''
    initial = 0
    def on_touch_down(self, touch):
        self.initial = touch.x

    def on_touch_up(self, touch):
        if touch.x > self.initial:
            # do something
        elif touch.x < self.initial:
            # do other thing
        else: 
            # what happens if there is no move
    '''
    pass

但是当我添加触摸事件以检测向右和向左滑动时,滚动将被禁用。现在我可以做左,右刷,但不能滚动。如何既可以滚动也可以滑动?

1 个答案:

答案 0 :(得分:1)

您正在使用此代码中断event bubbling,并且根本不会调用布局子项的on_touch_down方法。通常,您应该始终返回True或传递事件(或两者的某种组合)。

class FirstScreen(Screen):
    initial = 0
    def on_touch_down(self, touch):
        self.initial = touch.x
        return super(Screen, self).on_touch_down(touch) #pass the touch on

    def on_touch_up(self, touch):
        if touch.x > self.initial:
            print "left"
            return True #don't pass the touch on
        elif touch.x < self.initial:
            print "right"
            return True
        else: 
            print "no"
            return super(Screen, self).on_touch_up(touch)