我的 kivy Scrollview 在触摸事件和鼠标滚动事件中表现不同

My kivy Scrollview behaves differently in touch event and mouse scroll event

我在 VS 代码下使用 Kivy(v.1.10.1) 和 Python(v.3.6.6) 创建了一个 windows 应用程序。我想在 BoxLayout 下面使用 ScrollView。但是,它不能很好地工作。换句话说,它在使用鼠标滚动时滚动两次(附图),但在使用触摸屏时只滚动一次。我的目标是使用鼠标滚轮进行单次滚动。我应该怎么办?请帮助我!

我的基维密码

    #:kivy 1.10.1
<Test>:    
    BoxLayout:
        orientation:'vertical'
        size:root.size
        AnchorLayout:
            id:upper_bar
            anchor_x:'left'
            anchor_y:'top'
            ActionBar:
                size_hint_y:None
                size_hint_x:1
                height:30
                ActionView:
                    ActionPrevious:
                        with_previous: False
                        title:''
                        app_icon:''
                    ActionGroup:
                        text:'File'
                        mode:'spinner'
                        ActionButton:
                            text:'Open'
                        ActionButton:
                            text:'Add New Obj'

        AnchorLayout:
            id:main_field
            anchor_x:'center'
            anchor_y:'center'
            size_hint_y:None
            size_hint_x:1
            height:root.height - upper_bar.height - lower_bar.height
            ScrollView:
                id:main_field
                do_scroll_y:True
                pos_hint: {'top': 1}
                ScatterLayout:
                    size_hint:[None,None]
                    size:1000,1000
                    canvas:
                        Rectangle: 
                            pos:10,10
                            size:100,200
                        Rectangle: 
                            pos:300,150
                            size:100,200

        AnchorLayout:
            id:lower_bar
            size_hint_y:None
            size_hint_x:1
            height:30
            anchor_x:'left'
            anchor_y:'bottom'
            Button:
                text:'lower bar'

我的python代码

    #:Python 3.6.6
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
Builder.load_file('Test.kv')
class UpperBar(Widget):
    pass

class MainField(Widget):
    pass

class LowerBar(Widget):
    pass

class Test(Widget):
    pass

class TestApp(App):
    def build(self):
        return Test()
    pass

if __name__ == '__main__':
    TestApp().run()

我上面代码的结果

result of ikolim's suggestion

scroll_type 是一个 OptionProperty,默认为 [‘content’]

尝试将 effect_cls: "ScrollEffect", scroll_type: ['bars'], and bar_width: 8 添加到 ScrollView。

片段

        ScrollView:
            id:main_field
            do_scroll_y:True
            pos_hint: {'top': 1}

            bar_width: 8
            bar_color: 1, 0, 0, 1   # red
            bar_inactive_color: 0, 0, 1, 1   # blue
            effect_cls: "ScrollEffect"
            scroll_type: ['bars']

例子

main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder

Builder.load_file('main.kv')


class UpperBar(Widget):
    pass


class MainField(Widget):
    pass


class LowerBar(Widget):
    pass


class Test(Widget):
    pass


class TestApp(App):
    def build(self):
        return Test()


if __name__ == '__main__':
    TestApp().run()

main.kv

#:kivy 1.10.1

<Test>:
    BoxLayout:
        orientation:'vertical'
        size:root.size

        AnchorLayout:
            id:upper_bar
            anchor_x:'left'
            anchor_y:'top'
            ActionBar:
                size_hint_y:None
                size_hint_x:1
                height:30
                ActionView:
                    ActionPrevious:
                        with_previous: False
                        title:''
                        app_icon:''
                    ActionGroup:
                        text:'File'
                        mode:'spinner'
                        ActionButton:
                            text:'Open'
                        ActionButton:
                            text:'Add New Obj'

        AnchorLayout:
            id:main_field
            anchor_x:'center'
            anchor_y:'center'
            size_hint_y:None
            size_hint_x:1
            height:root.height - upper_bar.height - lower_bar.height

            ScrollView:
                id:main_field
                do_scroll_y:True
                pos_hint: {'top': 1}

                bar_width: 8
                bar_color: 1, 0, 0, 1   # red
                bar_inactive_color: 0, 0, 1, 1   # blue
                effect_cls: "ScrollEffect"
                scroll_type: ['bars']

                ScatterLayout:
                    size_hint:[None,None]
                    size:1000,1000
                    canvas:
                        Rectangle:
                            pos:10,10
                            size:100,200
                        Rectangle:
                            pos:300,150
                            size:100,200

        AnchorLayout:
            id:lower_bar
            size_hint_y:None
            size_hint_x:1
            height:30
            anchor_x:'left'
            anchor_y:'bottom'

            Button:
                text:'lower bar'

输出

我认为 ikolim 通过查看设置一些滚动视图属性走在了正确的轨道上。尝试设置 scroll_wheel_distance。在 kivy 文档中,它被描述为使用鼠标滚轮滚动时移动的距离。它的默认值为 20,因此请尝试将其减半为 10!希望这将使您的鼠标滚轮滚动与触摸屏滚动相同的距离!