我想从.py文件中删除此行:
self.layout.bind(minimum_height=self.layout.setter('height'))
并使用相同的指令替换它,但在.kv文件中。有没有办法将self.layout.setter('height')
绑定到minimum_height
?
答案 0 :(得分:1)
要使布局高度适应子窗口小部件的高度,您只需执行以下操作:
height: self.minimum_height
示例:强>
仅限python代码:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
class RootWidget(BoxLayout):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
self.orientation = "vertical"
self.sv = ScrollView()
self.btn = Button(text="Addd button", size_hint_y=0.1,
on_press = self.add_button)
self.layout = GridLayout(cols = 1, size_hint_y= None,
row_default_height= 100)
self.layout.bind(minimum_height=self.layout.setter('height'))
self.add_widget(self.sv)
self.add_widget(self.btn)
self.sv.add_widget(self.layout)
def add_button(self, instance):
self.layout.add_widget(Button())
class TestApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
TestApp().run()
使用Kivy Languaje:
<强> main.py:强>
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class RootWidget(BoxLayout):
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
def add_button(self):
self.layout.add_widget(Button())
class TestApp(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
TestApp().run()
<强> test.kv:强>
<RootWidget>:
orientation: "vertical"
layout: glayout
ScrollView:
GridLayout
id: glayout
cols:1
row_default_height: 100
size_hint_y: None
height: self.minimum_height # <<<<<<<<<<<<<<<<
Button:
text: "Add button"
on_press: root.add_button()
size_hint_y: 0.1
答案 1 :(得分:0)
怎么样?
height: self.minimum_height
我猜你在ScrollView中使用它。如果是真的,这应该有效。 例如,请参阅此帖子https://stackoverflow.com/a/45313388/6646710,该帖子正是如此。
答案 2 :(得分:0)
您可以在.kv文件中执行height: self.minimum_height
。请记住,有时您还必须设置size_hint_y: None
才能看到所需的效果。