如何在 kivy python 中使用滚动条
问题描述
谁能告诉我如何在这段代码中使用滚动条?其次,是否有任何方法可以对齐标签和 TextInput,以便 TextInput 中的文本无论有多少 Input 都将清晰可见.这里对齐意味着:如果有 100s(数百或数千)个 TextInputs ,则 TextInput 内的文本应该是正确可见的.实际上,当我在代码中给出一些(间距 = 50)时,在大约 20 秒的输入后,文本无法正常显示.提前致谢.
从 kivy.app 导入 App从 kivy.uix.tabbedpanel 导入 TabbedPanel从 kivy.uix.boxlayout 导入 BoxLayout从 kivy.properties 导入 StringProperty从 kivy.uix.textinput 导入 TextInput从 kivy.uix.checkbox 导入 CheckBox从 kivy.lang 导入生成器ROWS = ['Goc','COC','EEE','abs','kju','iop','nmg','gty','jkio','dbkgcd','udbcbjkb']builder.load_string("""<测试>:do_default_tab: 假选项卡面板项:文本:'page1'桌子:填充:50、50、50、50方向:垂直"<行>:间距:50size_hint_x: 1txt: txtinpt.text标签:文本:root.txt文本输入:编号:txtinpt文本:root.txt禁用:不是 CheckBox.active复选框:id:复选框文本:'复选框'活跃:假按钮:文字:'保存'""")类表(BoxLayout):def __init__(self, **kwargs):超级(表,自我).__init__(**kwargs)对于 ROWS 中的行:self.add_widget(行(行))类行(BoxLayout):txt = 字符串属性()def __init__(self, row, **kwargs):超级(行,自我).__init__(**kwargs)self.txt = 行类测试(TabbedPanel):经过我的应用程序(应用程序)类:定义构建(自我):测试 = 测试()返回测试如果 __name__ == '__main__':MyApp().run()
解决方案 接下来的步骤是:
- 在
TabbedPanelItem
中添加一个ScrollView
并将Table
放入其中. - 使用
size_hint_y = None
防止 boxlayout (Table
) 自动调整其高度以适应其父窗口小部件的高度. - 指定
行
高度.
代码可能是:
从 kivy.app 导入 App从 kivy.uix.tabbedpanel 导入 TabbedPanel从 kivy.uix.boxlayout 导入 BoxLayout从 kivy.properties 导入 StringProperty从 kivy.uix.textinput 导入 TextInput从 kivy.uix.checkbox 导入 CheckBox从 kivy.lang 导入生成器ROWS = ['Goc','COC','EEE','abs','kju','iop','nmg','gty','jkio','dbkgcd','udbcbjkb']builder.load_string("""<测试>:do_default_tab: 假选项卡面板项:文本:'page1'滚动视图:桌子:方向:垂直"size_hint_y:无高度:self.minimum_height填充:50、50、50、50<行>:间距:50size_hint_y:无size_hint_x: 1身高:100txt: txtinpt.text标签:文本:root.txt文本输入:编号:txtinpt文本:root.txt禁用:不是 CheckBox.active复选框:id:复选框文本:'复选框'活跃:假按钮:文字:'保存'""")类表(BoxLayout):def __init__(self, **kwargs):超级(表,自我).__init__(**kwargs)对于 ROWS 中的行:self.add_widget(行(行))类行(BoxLayout):txt = 字符串属性()def __init__(self, row, **kwargs):超级(行,自我).__init__(**kwargs)self.txt = 行类测试(TabbedPanel):经过我的应用程序(应用程序)类:定义构建(自我):测试 = 测试()返回测试如果 __name__ == '__main__':MyApp().run()
<块引用>
注意:修改只影响kv文件.
输出:
Can anybody tell me how to use scrollbar in this code? And secondly, is there any way to align the labels and TextInput so that text inside the TextInput will be visible clearly no matter how much Input will be there. Here alignment means: if there is 100s(hundreds or thousands) of TextInputs , the text inside TextInput should be properly visible. Actually when I was giving some (spacing = 50) in the code, after some 20s input, the text was not visible properly. Thanks in advance.
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.lang import Builder
ROWS = ['Goc', 'COC', 'EEE', 'abs' , 'kju' , 'iop' , 'nmg', 'gty', 'jkio', 'dbkgcd' , 'udbcbjkb']
Builder.load_string("""
<Test>:
do_default_tab: False
TabbedPanelItem:
text: 'page1'
Table:
padding: 50, 50, 50, 50
orientation: 'vertical'
<Row>:
spacing: 50
size_hint_x: 1
txt: txtinpt.text
Label:
text: root.txt
TextInput:
id: txtinpt
text: root.txt
disabled: not CheckBox.active
CheckBox:
id:CheckBox
text: 'CheckBox'
active: False
Button:
text: 'save'
""")
class Table(BoxLayout):
def __init__(self, **kwargs):
super(Table, self).__init__(**kwargs)
for row in ROWS:
self.add_widget(Row(row))
class Row(BoxLayout):
txt = StringProperty()
def __init__(self, row, **kwargs):
super(Row, self).__init__(**kwargs)
self.txt = row
class Test(TabbedPanel):
pass
class MyApp(App):
def build(self):
test = Test()
return test
if __name__ == '__main__':
MyApp().run()
解决方案 The steps to follow are:
- Add a
ScrollView
toTabbedPanelItem
and putTable
inside it. - Prevent the boxlayout (
Table
) from automatically adapting its height to the height of its parent widget usingsize_hint_y = None
. - Specify
Row
height.
The code could be:
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.lang import Builder
ROWS = ['Goc', 'COC', 'EEE', 'abs' , 'kju' , 'iop' , 'nmg', 'gty', 'jkio', 'dbkgcd' , 'udbcbjkb']
Builder.load_string("""
<Test>:
do_default_tab: False
TabbedPanelItem:
text: 'page1'
ScrollView:
Table:
orientation: "vertical"
size_hint_y: None
height: self.minimum_height
padding: 50, 50, 50, 50
<Row>:
spacing: 50
size_hint_y: None
size_hint_x: 1
height: 100
txt: txtinpt.text
Label:
text: root.txt
TextInput:
id: txtinpt
text: root.txt
disabled: not CheckBox.active
CheckBox:
id:CheckBox
text: 'CheckBox'
active: False
Button:
text: 'save'
""")
class Table(BoxLayout):
def __init__(self, **kwargs):
super(Table, self).__init__(**kwargs)
for row in ROWS:
self.add_widget(Row(row))
class Row(BoxLayout):
txt = StringProperty()
def __init__(self, row, **kwargs):
super(Row, self).__init__(**kwargs)
self.txt = row
class Test(TabbedPanel):
pass
class MyApp(App):
def build(self):
test = Test()
return test
if __name__ == '__main__':
MyApp().run()
Note: The modifications only affect kv file.
Output:
相关文章