【问题标题】:Make a scrollview scroll underneath another widget in kivy在 kivy 中的另一个小部件下方滚动滚动视图
【发布时间】:2018-04-11 19:59:13
【问题描述】:

我想在 kivy 的滚动视图网格布局顶部覆盖一个矩形、按钮和标签,以提供带有当前屏幕名称的横幅和导航弹出窗口的按钮。我制作了一个按钮网格布局列表,用于链接到拼图应用中未来的小游戏。

我要放在最上面的内容在screenmanager.kv里面,而gridlayout在gameslist.kv里面。

一切都显示在屏幕上。网格布局垂直滚动。但是,它会滚动 screenmanager.kv 的内容,而不是在其下方。我希望横幅保持可见,浮动在网格上,始终位于窗口顶部的同一位置,尽管用户在网格布局中滚动了哪里。

screenmanager.kv:

<GamesListScreen>:
    canvas.before:
        Color:
            rgba: 1, 0.6, 0.6, 1
        Rectangle:
            size: self.width, (self.height * 0.15)
            pos: 0, (self.height * 0.85)
    Button:
        color: 1, 1, 1, 0
        size_hint: None, None
        height: Window.height * 0.15
        width: self.height
        pos: 0, (root.top * 0.85)
        on_release:
            root.open_NavPopup()
            print('pressed')
        Image:
            source: 'images/NavIcon.png'
            keep_ratio: False
            allow_stretch: True
            y: self.parent.y
            x: self.parent.x
            size: self.parent.size

    Label:
        font_size: self.width * 0.1
        color: 1,1,1,1
        center_x: self.width / 2
        y: self.height / 2.35
        text: 'Games List'

gameslist.kv:

#:import ScrollEffect kivy.effects.scroll.ScrollEffect
#:import Button kivy.uix.button.Button

<RootWidget>
    effect_cls: ScrollEffect
    GridLayout:
        do_scroll_x: False
        height: self.minimum_height
        size_hint: 1, None
        size: Window.width, (Window.height * .85)
        spacing: 5, 5
        padding: [0,(Window.height * .17),0,0]
        cols: 1
        on_parent:
            self.add_widget(Button(text = "Game 1", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 2", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 3", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 4", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 5", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 6", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 7", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 8", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 9", size_hint_y = None, height = Window.height * .1))
            self.add_widget(Button(text = "Game 10", size_hint_y = None, height = Window.height * .1))

gameslist.py,以防万一:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.scrollview import ScrollView
Builder.load_file('gameslist.kv')

class RootWidget(ScrollView):
    pass

class runGamesList(App):
    def build(self):
        root = RootWidget()
        return root

我尝试将 canvas.before 更改为 canvas.after 并将叠加层放在 gridlayout 内,以便它随之滚动。都没有奏效。有小费吗?还是我应该放弃并做一些在 x 轴上滚动的事情?

【问题讨论】:

    标签: android python python-3.x kivy


    【解决方案1】:

    有关详细信息,请参阅示例和输出。

    gameslist.py

    1. 添加从屏幕管理器导入 GamesListScreen
    2. 将根小部件从 ScrollView 更改为 BoxLayout

    gameslist.kv

    1. 添加#:include screenmanager.kv
    2. 将 GamesListScreen 声明为 RootWidget 的子小部件
    3. 对 ScrollView 进行更改

    示例

    gameslist.py

    ​​>
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from screenmanager import GamesListScreen
    
    
    class RootWidget(BoxLayout):
        pass
    
    
    class GamesListApp(App):
        def build(self):
            return RootWidget()
    
    
    if __name__ == "__main__":
        GamesListApp().run()
    

    gameslist.kv

    #:import ScrollEffect kivy.effects.scroll.ScrollEffect
    #:import Button kivy.uix.button.Button
    #:import Window kivy.core.window.Window
    #:include screenmanager.kv
    
    <RootWidget>:
        orientation: 'vertical'
    
        GamesListScreen:
    
        ScrollView:
            effect_cls: ScrollEffect
            size_hint: 1, None
            size: Window.width, (Window.height * .85)
    
            GridLayout:
                do_scroll_x: False
                height: self.minimum_height
                size_hint_y: None
                spacing: 5, 5
                cols: 1
                on_parent:
                    for i in range(10): txt = "Game {}".format(i); self.add_widget(Button(text = txt, size_hint_y = None,
                    height = Window.height * .1))
    

    输出

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2011-06-23
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多