简体   繁体   English

如何在屏幕上放置Kivy ScrollView

[英]How to put Kivy ScrollView on a Screen

Kivy Code: Kivy代码:

GridLayout:

    Button:

    ScrollView:

Python Code: Python代码:

class SettingsScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='start'))
sm.add_widget(NormalScreen(name='game'))

class ChatBot(App):

    def build(self):
        return sm

ChatBot().run()

I am trying to build my first GUI . 我正在尝试构建我的第一个GUI But the problem is, that my ScrollView is not working. 但是问题是我的ScrollView无法正常工作。 I need it to be on my normal Screen. 我需要将其显示在常规屏幕上。 Anyone knowing how to fix this problem? 任何人都知道如何解决此问题?

The example illustrates a Scrollview of Stacklayout with Labels. 该示例说明了带有标签的Stacklayout的Scrollview。 Please refer to the example for details. 有关详细信息,请参阅示例。 The following Scrollview APIs were used in the example. 在示例中使用了以下Scrollview API。

ScrollView 滚动视图

layout.bind(minimum_height=layout.setter('height')) layout.bind(minimum_height = layout.setter('height'))

bar_color bar_color
Color of horizontal / vertical scroll bar, in RGBA format. 水平/垂直滚动条的颜色,采用RGBA格式。
bar_color is a ListProperty and defaults to [.7, .7, .7, .9]. bar_color是ListProperty,默认为[.7,.7,.7,.9]。

bar_inactive_color bar_inactive_color
Color of horizontal / vertical scroll bar (in RGBA format), when no scroll is happening. 当没有滚动发生时,水平/垂直滚动条的颜色(RGBA格式)。
bar_inactive_color is a ListProperty and defaults to [.7, .7, .7, .2]. bar_inactive_color是一个ListProperty,默认为[.7,.7,.7,.2]。

bar_width bar_width
Width of the horizontal / vertical scroll bar. 水平/垂直滚动条的宽度。 The width is interpreted as a height for the horizontal bar. 宽度被解释为单杠的高度。
bar_width is a NumericProperty and defaults to 2. bar_width是NumericProperty,默认为2。

effect_cls effect_cls
Class effect to instantiate for X and Y axis. 为X和Y轴实例化的类效果。
effect_cls is an ObjectProperty and defaults to DampedScrollEffect. effect_cls是一个ObjectProperty,默认为DampedScrollEffect。

scroll_type scroll_type
Sets the type of scrolling to use for the content of the scrollview. 设置用于滚动视图内容的滚动类型。 Available options are: ['content'], ['bars'], ['bars', 'content']. 可用选项包括:['content'],['bars'],['bars','content']。

['content'] Content is scrolled by dragging or swiping the content directly. ['content']通过直接拖动或滑动内容来滚动内容。
['bars'] Content is scrolled by dragging or swiping the scoll bars. ['bars']通过拖动或滑动scoll栏可以滚动内容。
['bars', 'content'] Content is scrolled by either of the above methods. ['bars','content']通过以上两种方法之一滚动内容。

scroll_type is an OptionProperty and defaults to ['content']. scroll_type是一个OptionProperty,默认为['content']。

Example

main.py 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()

chatbot.kv chatbot.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()

Output 输出量

Img01-滚动视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM