如何将Kivy ScrollView放在屏幕上

时间:2018-05-29 12:51:47

标签: python user-interface kivy kivy-language

Kivy Code:

GridLayout:

    Button:

    ScrollView:

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()

我正在尝试构建我的第一个GUI。但问题是,我的ScrollView无法正常工作。我需要它在我的普通屏幕上。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

该示例说明了带有标签的Stacklayout的Scrollview。有关详细信息,请参阅示例。示例中使用了以下Scrollview API。

ScrollView

  

layout.bind(minimum_height = layout.setter('高度'))

     

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'],   ['bars','content']。

     

['content']通过拖动或滑动内容来滚动内容   直接。
['bars']通过拖动或滑动滚动内容   scoll bars。
['bars','content']内容由任意一个滚动   上述方法。

scroll_type是OptionProperty和默认值   到['content']。

实施例

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

#: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()

输出

Img01 - Scrollview