如何在kivy python中使用滚动条 [英] how to use scrollbar in kivy python
问题描述
有人可以告诉我如何在此代码中使用滚动条吗?其次,有什么方法可以使标签和TextInput对齐,从而无论输入多少,都可以清楚地看到TextInput中的文本.这里的对齐方式是:如果有100s(数百或数千)的TextInputs,则TextInput中的文本应正确可见.实际上,当我在代码中输入一些内容(间距= 50)时,输入20秒钟后,该文本就无法正确显示. 提前致谢.
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()
推荐答案
要遵循的步骤是:
- 在
TabbedPanelItem
中添加ScrollView
并将Table
放入其中. - 防止BoxLayout(
Table
)使用size_hint_y = None
自动将其高度调整为其父窗口小部件的高度. - 指定
Row
高度.
- 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.
代码可以是:
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()
注意::修改仅影响kv文件.
Note: The modifications only affect kv file.
输出:
这篇关于如何在kivy python中使用滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!