我已经在这几个小时了,尝试我能在这里找到的所有解决方案并尝试随机的事情.... 我正在尝试构建一个由顶部的3个按钮组成的布局,然后是一个可滚动的GridLayout或BoxLayout。我只是想弄清楚什么是错的......我读过一个回复“绑定布局的大小以适应自己:”但我正在使用屏幕管理而无法弄清楚如何使用我的代码设置来做到这一点
<HomeScreen>:
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: 1,.1
orientation: "horizontal"
Button:
text:"1"
Button:
text:"2"
Button:
text:"3"
ScrollView:
GridLayout:
orientation: "vertical"
size_hint_y: None
row_default_height: 60
cols:1
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
答案 0 :(得分:0)
您的代码是正确的,您只需要指定GridLayout的高度。您可以使用height: self.minimum_height
。
可重复的例子:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
kv_text = '''
<MyScreenManager>:
HomeScreen:
<HomeScreen>:
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: 1,.1
orientation: "horizontal"
Button:
text:"1"
Button:
text:"2"
Button:
text:"3"
ScrollView:
GridLayout:
orientation: "vertical"
size_hint_y: None
height: self.minimum_height #<<<<<<<<<<<<<<<<<<<<
row_default_height: 60
cols:1
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
Button:
'''
class MyScreenManager(ScreenManager):
pass
class HomeScreen(Screen):
pass
class MyApp(App):
def build(self):
return HomeScreen()
def main():
Builder.load_string(kv_text)
app = MyApp()
app.run()
if __name__ == '__main__':
main()
输出:
答案 1 :(得分:0)
我想做与上面介绍的相同的事情,但使用纯Python(不使用kv lang),但是效果不好。我不知道怎么了。
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class HomeScreen(Screen):
def __init__(self, **kwargs):
super(HomeScreen, self).__init__(**kwargs)
box = BoxLayout()
self.add_widget(box)
scroll_view = ScrollView()
box.add_widget(scroll_view)
grid = ScrollGridLayout()
scroll_view.add_widget(grid)
for i in range(0, 30):
btn = Button()
grid.add_widget(btn)
class ScrollGridLayout(GridLayout):
def __init__(self, **kwargs):
super(ScrollGridLayout, self).__init__(**kwargs)
self.orientation = "vertical"
self.size_hint_y=None
self.height=self.minimum_height
self.row_default_height=100
self.cols=1
class MyApp(App):
def build(self):
return HomeScreen()
def main():
app = MyApp()
app.run()
if __name__ == '__main__':
main()