kivy 滚动视图中的可调文本输入
Adjustable textinput in kivy scrollview
我是 Kivy 的新手。我希望多个 textInputs 在 Kivy scrollView 中动态正确显示,其中 textInput 大小适合内容。当前代码仅显示 3,因为 gridLayout 的高度设置为 1000。我想使用 height: self.minimum_height 以便我可以动态添加更多 textInputs 并且它们将正确显示,但似乎无法获得它工作。
我的kv文件:
<MyNote@BoxLayout>:
orientation: 'vertical'
TextInput:
height: self.minimum_height
multiline: True
text_size: self.size
size_hint_y: None
text: '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15'
<MainNotesBoxLayout>
orientation: 'vertical'
ScrollView:
GridLayout:
cols: 1
size_hint_y: None
height: 1000
#height: self.minimum_height
MyNote:
MyNote:
MyNote:
我的主文件:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class MainNotesBoxLayout(BoxLayout):
pass
class NotesApp(App):
def build(self):
return MainNotesBoxLayout()
if __name__ == '__main__':
NotesApp().run()
- 用 BoxLayout 替换 GridLayout
- 设置框布局的最小高度,以便可以滚动。
layout.bind(minimum_height=layout.setter('height'))
例子
notes.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
class MyNote(TextInput):
pass
class MainNotesBoxLayout(BoxLayout):
container = ObjectProperty(None)
def __init__(self, **kwargs):
super(MainNotesBoxLayout, self).__init__(**kwargs)
self.container.bind(minimum_height=self.container.setter('height'))
self.add_text_inputs()
def add_text_inputs(self):
for x in range(100):
my_note = MyNote()
self.container.add_widget(my_note)
class NotesApp(App):
def build(self):
return MainNotesBoxLayout()
if __name__ == '__main__':
NotesApp().run()
notes.kv
#:kivy 1.10.0
<MyNote>:
height: self.minimum_height
multiline: True
text_size: self.size
size_hint_y: None
text: '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15'
<MainNotesBoxLayout>
container: container
orientation: 'vertical'
ScrollView:
size_hint: (1, 1)
bar_width: 10
bar_color: 1, 0, 0, 1 # red
bar_inactive_color: 0, 0, 1, 1 # blue
effect_cls: "ScrollEffect"
scroll_type: ['bars']
BoxLayout:
id: container
orientation: 'vertical'
size_hint: 1, None
输出
我是 Kivy 的新手。我希望多个 textInputs 在 Kivy scrollView 中动态正确显示,其中 textInput 大小适合内容。当前代码仅显示 3,因为 gridLayout 的高度设置为 1000。我想使用 height: self.minimum_height 以便我可以动态添加更多 textInputs 并且它们将正确显示,但似乎无法获得它工作。
我的kv文件:
<MyNote@BoxLayout>:
orientation: 'vertical'
TextInput:
height: self.minimum_height
multiline: True
text_size: self.size
size_hint_y: None
text: '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15'
<MainNotesBoxLayout>
orientation: 'vertical'
ScrollView:
GridLayout:
cols: 1
size_hint_y: None
height: 1000
#height: self.minimum_height
MyNote:
MyNote:
MyNote:
我的主文件:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class MainNotesBoxLayout(BoxLayout):
pass
class NotesApp(App):
def build(self):
return MainNotesBoxLayout()
if __name__ == '__main__':
NotesApp().run()
- 用 BoxLayout 替换 GridLayout
- 设置框布局的最小高度,以便可以滚动。
layout.bind(minimum_height=layout.setter('height'))
例子
notes.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
class MyNote(TextInput):
pass
class MainNotesBoxLayout(BoxLayout):
container = ObjectProperty(None)
def __init__(self, **kwargs):
super(MainNotesBoxLayout, self).__init__(**kwargs)
self.container.bind(minimum_height=self.container.setter('height'))
self.add_text_inputs()
def add_text_inputs(self):
for x in range(100):
my_note = MyNote()
self.container.add_widget(my_note)
class NotesApp(App):
def build(self):
return MainNotesBoxLayout()
if __name__ == '__main__':
NotesApp().run()
notes.kv
#:kivy 1.10.0
<MyNote>:
height: self.minimum_height
multiline: True
text_size: self.size
size_hint_y: None
text: '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15'
<MainNotesBoxLayout>
container: container
orientation: 'vertical'
ScrollView:
size_hint: (1, 1)
bar_width: 10
bar_color: 1, 0, 0, 1 # red
bar_inactive_color: 0, 0, 1, 1 # blue
effect_cls: "ScrollEffect"
scroll_type: ['bars']
BoxLayout:
id: container
orientation: 'vertical'
size_hint: 1, None