在kivy中的另一个小部件下滚动滚动视图

时间:2018-04-11 19:59:13

标签: android python python-3.x kivy

我想在kivy的scrollview gridlayout上叠加一个矩形,按钮和标签,以提供一个横幅,其中包含当前屏幕的名称和导航弹出窗口的按钮。我已经制作了一个gridlayout按钮列表,用于链接到拼图应用程序中的未来迷你游戏。

我想放在顶部的内容是在screenmanager.kv中,而gridlayout在gameslist.kv中。

所有内容都显示在屏幕上。 gridlayout垂直滚动。但是,它滚动 over screenmanager.kv的内容,而不是它下面的内容。我希望横幅保持可见,漂浮在网格上,持久地位于窗口顶部的同一位置,尽管用户已在gridlayout中滚动。

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轴上滚动的东西?

1 个答案:

答案 0 :(得分:1)

请参阅示例和输出以获取详细信息。

gameslist.py

  1. 从screenmanager导入GamesListScreen
  2. 添加
  3. 将根小部件从 ScrollView 更改为 BoxLayout
  4. gameslist.kv

    1. 添加#:include screenmanager.kv
    2. 将GamesListScreen声明为RootWidget的子窗口小部件
    3. 更改ScrollView
    4. 实施例

      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))
      

      输出

      Img01 - App Startup Img02 - Scroll Img03 - Navigation Button clicked