该示例说明了带有标签的 Stacklayout 的滚动视图。详情请参阅示例。示例中使用了以下 Scrollview API。
ScrollView
layout.bind(minimum_height=layout.setter('height'))
bar_color
水平/垂直滚动条的颜色,RGBA格式。
bar_color 是一个 ListProperty,默认为 [.7, .7, .7, .9]。
bar_inactive_color
没有滚动时水平/垂直滚动条的颜色(RGBA 格式)。
bar_inactive_color 是一个 ListProperty,默认为 [.7, .7, .7, .2]。
bar_width
水平/垂直滚动条的宽度。宽度被解释为水平条的高度。
bar_width 是一个 NumericProperty,默认为 2。
effect_cls
用于实例化 X 和 Y 轴的类效果。
effect_cls 是一个 ObjectProperty,默认为 DampedScrollEffect。
scroll_type
设置用于滚动视图内容的滚动类型。可用选项有:['content']、['bars']、
['条','内容']。
['content'] 通过拖动或滑动内容来滚动内容
['bars'] 通过拖动或滑动来滚动内容
scoll bar.
['bars', 'content'] 内容滚动
上述方法。
scroll_type 是一个 OptionProperty 和默认值
到[‘内容’]。
示例
main.py
>
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.clock import Clock
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
container = ObjectProperty(None)
def __init__(self, **kwargs):
super(SettingsScreen, self).__init__(**kwargs)
Clock.schedule_once(self.setup_scrollview, 1)
def setup_scrollview(self, dt):
self.container.bind(minimum_height=self.container.setter('height'))
self.add_text_inputs()
def add_text_inputs(self):
for x in range(30):
self.container.add_widget(Label(text="Label {}".format(x), size_hint_y=None, height=40))
def new_message(self):
msg = self.display.text
print(msg)
class ScreenManagement(ScreenManager):
pass
class ChatBot(App):
def build(self):
return ScreenManagement()
ChatBot().run()
聊天机器人.kv
#:kivy 1.10.0
<But@Button>:
font_size: 20
font_name: "Calibri"
color: 0, 0, 0, 1
size_hint: .7, .1
background_normal: ''
background_down: 'test.png'
background_color: .88,.88,.88, 1
<Lab@Label>:
font_size: 27
font_name: "Calibri"
color: 0, 0, 0, 1
<Grid@GridLayout>:
<ScreenManagement>:
MenuScreen:
name: 'start'
SettingsScreen:
name: 'game'
<MenuScreen>:
FloatLayout:
canvas.before:
BorderImage:
border: 10, 10, 10, 10
source: 'Blur-4K-Img08.jpeg' # 'Blur-4K-Abstract-Wallpaper.png'
pos: self.pos
size: self.size
But:
text: "START"
pos_hint: {"center_x": .5, "center_y": .3}
on_press: root.manager.current = 'game'
Lab:
text: "Welcome to my ChatBot!"
pos_hint: {"center_x": .5, "center_y": .8}
<SettingsScreen>:
display: entry
message: send
container: container
FloatLayout:
canvas.before:
BorderImage:
border: 10, 10, 10, 10
source: 'Blur-4K-Img08.jpeg' # 'Blur-4K-Abstract-Wallpaper.png'
pos: self.pos
size: self.size
GridLayout:
rows: 3
cols: 1
spacing: 5
padding: 5
font_name: "Calibri"
Button:
text: "ChatBot"
color: 0, 0, 0, 1
size_hint: .7, .1
background_normal: ''
background_down: ''
background_color: .88,.88,.88, 1
font_size: 20
ScrollView:
size_hint: (1, .9)
bar_width: 10
bar_color: 1, 0, 0, 1 # red
bar_inactive_color: 0, 0, 1, 1 # blue
effect_cls: "ScrollEffect"
scroll_type: ['bars']
StackLayout:
id: container
size_hint_y: None
BoxLayout:
spacing: 5
size_hint: .7, .1
TextInput:
id: entry
multiline: False
font_size: 25
Button:
id: send
color: 0, 0, 0, 1
background_normal: 'send.jpg'
background_down: 'test.png'
background_color: .88,.88,.88, 1
size_hint: .2, 1
on_press: root.new_message()
输出