我的kivy Scrollview在触摸事件和鼠标滚动事件中的行为有所不同

时间:2019-03-21 12:24:07

标签: python kivy scrollview mousewheel

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

我的上位代码结果 my result of upper code

result of ikolim's suggestion

2 个答案:

答案 0 :(得分:0)

scroll_typeOptionProperty,默认为[‘content’]

尝试将effect_cls: "ScrollEffect"scroll_type: ['bars']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'

输出

ScrollView

答案 1 :(得分:0)

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