ScrollView 中没有滚动的按钮列表
No scrolling list of buttons in ScrollView
我有一个界面布局(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()
我想要得到这个结果:
运行 此脚本在此处显示以下结果:
并且列表没有滚动按钮。我哪里出错了?
您在 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)
我有一个界面布局(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()
我想要得到这个结果:
运行 此脚本在此处显示以下结果:
并且列表没有滚动按钮。我哪里出错了?
您在 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)