【问题标题】:No scrolling list of buttons in ScrollViewScrollView 中没有按钮的滚动列表
【发布时间】:2016-04-28 10:23:09
【问题描述】:

我有一个界面布局(cleanscreen.kv):

#:kivy 1.9.1

<CleanScreen>
    FloatLayout:
        canvas:
            Color:
                rgb:
                    0.1, 0.3, 0.6
            Rectangle:
                pos: self.pos
                size: self.size

    ScrollView:
        GridLayout:
            cols: 1
            size_hint_y: None
            spacing: 10
            padding: 10
            height: self.minimum_height
            canvas:
                Color:
                    rgb: 1, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
            FloatLayout:
                id: box_share
                size_hint_y: None

和 Python 文件(cleanscreen.py):

#! /usr/bin/python2.7
# -*- coding: utf-8 -*-

try:
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.image import Image
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.lang import Builder
except Exception as text_error:
    raise text_error


class CleanScreen(BoxLayout):
    Builder.load_file("cleanscreen.kv")

    def __init__(self, **kvargs):
        super(CleanScreen, self).__init__(**kvargs)
        self.orientation = "vertical"
        self.create_button(self.ids.box_share)

    def create_button(self, box_share):
        top_logo_share = 1.01
        top_button_share = 1.1
        top_label_share = 1.4

        for i in range(50):
            top_logo_share -= .4
            top_button_share -= .4
            top_label_share -= .4

            logo_share = \
                Image(source="data/logo/kivy-icon-48.png",
                      pos_hint={"center_x": .05, "top": top_logo_share},
                      size_hint_y=None, height=25)
            button_share = \
                Button(pos_hint={"x": 0, "top": top_button_share},
                       size_hint_y=None, height=40)
            label_share = \
                Label(text=str(i), pos_hint={"x": 0, "top": top_label_share},
                      size_hint_y=None)

            box_share.add_widget(button_share)
            box_share.add_widget(logo_share)
            box_share.add_widget(label_share)

if __name__ in ["__main__", "__android__"]:
    import kivy
    kivy.require("1.9.1")


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


    Test().run()

我想得到这个结果:

运行此脚本在此处显示以下结果:

并且列表没有滚动按钮​​。我哪里出错了?

【问题讨论】:

    标签: python-2.7 kivy kivy-language


    【解决方案1】:

    您在 kv 文件中的 FloatLayout 没有设置高度,因此获取默认值。这实际上并不重要,因为您只需将小部件推入一个小部件中,当它在 GridLayout 内部时,它在 ScrollView 内部时无济于事。不,有更好更合理的方法(至少我是这么看的)。 :)

    从 kv 文件中丢弃FloatLayout,没有必要使用GridLayout,因为当您将小部件推入时,它应该变得更大(并影响ScrollView),而不是FloatLayout并迫使 GridLayout 人为地让自己变大。

    ScrollView:
        GridLayout:
            id: box_share
            cols: 1
            size_hint_y: None
            spacing: 10
            padding: 10
            height: self.minimum_height
            canvas:
                Color:
                    rgb: 1, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
    

    并在 python 文件中使用create_button() 创建一个FloatLayout,您可以在其中填充您的小部件。我对那个定位有点困惑,所以我不会为你解决这个问题,抱歉。不要忘记设置FloatLayout的高度。

    def create_button(self, box_share):
        top_logo_share = 1.01
        top_button_share = 1.1
        top_label_share = 1.4
    
        for i in range(50):
            top_logo_share -= .4
            top_button_share -= .4
            top_label_share -= .4
    
            logo_share = \
                Image(source="data/logo/kivy-icon-48.png",
                      pos_hint={"center_x": .05, "top": top_logo_share},
                      size_hint_y=None, height=25)
            button_share = \
                Button(pos_hint={"x": 0, "top": top_button_share},
                       size_hint_y=None, height=40)
            label_share = \
                Label(text=str(i), pos_hint={"x": 0, "top": top_label_share},
                      size_hint_y=None)
            fl = FloatLayout(size_hint_y=None, height=25)
            fl.add_widget(button_share)
            fl.add_widget(logo_share)
            fl.add_widget(label_share)
            box_share.add_widget(fl)
    

    【讨论】:

      猜你喜欢
      • 2011-03-31
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多