【问题标题】:How to properly use ScrollView in Kivy Python?如何在 Kivy Python 中正确使用 ScrollView?
【发布时间】:2020-04-03 05:46:47
【问题描述】:

我有这段代码,我想更改按钮的位置,但如果我更改位置,滚动将不再起作用。如何设法使其发挥作用?以下是工作版本。如果我将 size_hint: 1, .1 更改为 size_hint: 1, .7,则滚动不再起作用...

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string('''
<Root>:
    ScrollView:
        size_hint: 1, .1
        GridLayout:
            size_hint_y: None
            cols: 1
            # minimum_height: self.height
            Button
                text: 'one'
            Button:
                text: 'two'
            Button:
                text: 'three'
            Button:
                text: 'four'
''')

class Root(FloatLayout):
    pass

class DemoApp(App):

    def build(self):
        return Root()

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


【问题讨论】:

    标签: python scroll grid kivy grid-layout


    【解决方案1】:

    你做得对。 ScrollView 允许您滚动查看不适合ScrollViewGridLayout 部分。当您将size_hint 设置为(1, .7) 时,所有内容都适合ScrollView,因此不会滚动。

    您可以通过添加Widgets 来强制滚动以占用更多空间(例如Labels 没有文字):

    <Root>:
        ScrollView:
            size_hint: 1, .7
            GridLayout:
                size_hint_y: None
                cols: 1
                height: self.minimum_height
                Label:
                    text: ''
                    size_hint_y: None
                    height: 300
                Button
                    text: 'one'
                    size_hint: 1, None
                    height: self.texture_size[1]
                Button:
                    text: 'two'
                    size_hint: 1, None
                    height: self.texture_size[1]
                Button:
                    text: 'three'
                    size_hint: 1, None
                    height: self.texture_size[1]
                Button:
                    text: 'four'
                    size_hint: 1, None
                    height: self.texture_size[1]
                Label:
                    text: ''
                    size_hint_y: None
                    height: 300
    

    【讨论】:

      【解决方案2】:

      一旦父窗口小部件中有一个size_hint_y = None,那么您必须手动声明子窗口小部件的高度,因为 size_hint_y 处理高度或 Y 轴.. 所以我将您的代码修改为让事情变得更清晰

      from kivy.app import App
      from kivy.lang.builder import Builder
      from kivy.uix.floatlayout import FloatLayout
      
      Builder.load_string('''
      <Root>:
          ScrollView:
              size_hint_y:None
              height:root.width / 2
              GridLayout:
                  size_hint_y: None
                  cols: 1
                  height:self.minimum_height
                  spacing:dp(20)
                  Button
                      text: 'one'
                      size_hint_y:None
                      height:dp(50)
                  Button:
                      text: 'two'
                      size_hint_y:None
                      height:dp(50)
                  Button:
                      text: 'three'
                      size_hint_y:None
                      height:dp(50)
                  Button:
                      text: 'four'
                      size_hint_y:None
                      height:dp(50)
      ''')
      
      class Root(FloatLayout):
          pass
      
      class DemoApp(App):
      
          def build(self):
              return Root()
      
      if __name__ == '__main__':
          DemoApp().run()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        • 2014-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多