向下滚动屏幕以获取更长的文本,例如 kivy 中的故事

Posted

技术标签:

【中文标题】向下滚动屏幕以获取更长的文本,例如 kivy 中的故事【英文标题】:scroll down screen for longer text like stories in kivy 【发布时间】:2020-08-03 09:46:39 【问题描述】:

我在 kivy 中有一个屏幕,我想滚动它,因为标签小部件中的文本没有显示所有文本,就像在 kindle 中我们可以向下滚动故事一样。另外,我的 MainScreen 是从 Screen 类继承的。随着文本的增加,它不会显示故事的最后 5 或 6 行

这是我的那个类的 .kv 代码

```
<MainScreen>:
    BoxLayout:
        canvas:
            Color:
                rgb: 0, 0, 0, 0
            Rectangle:
                size: self.size

        Label:
            id: story
            font_size: '20sp'
            height: self.texture_size[1]
            size: self.texture_size
            text_size: self.width, self.height
            halign: "auto"
            valign: "center"
            pos_hint: "center_x": 0.7, "center_y": 0.49

            text: 

"""A boy and a girl were playing together. The boy had a collection of marbles. The girl has some 
sweets with her. The boy told the girl that he would give her all his marbles in exchange for the 
sweets with her. The girl agreed.

The boy kept the most beautiful and the biggest marbles with him and gave her the remaining marbles. 
The girl gave him all her sweets as she promised. That night the girl slept peacefully. But the boy 
could not sleep as he kept wondering if the girl has hidden some sweets from him the way he had 
hidden the best marbles from her.

Moral of the Story :

If you do not give 100 percent in a relationship, you will always kept doubting if the other person 
has given her / his hundred percent. This is applicable for any relationship like love, employee – 
employer, friendship, family, countries, etc…
"""
            ```

这是 .py 文件中的 MainScreen 类

    class MainScreen(Screen):
        pass

【问题讨论】:

你试过把它放在ScrollView吗? 但是放在哪里 【参考方案1】:

要制作任何东西scroll,您需要使用ScrollView 小部件。

这是如何在您发布的代码上实现的

ScrollView:
    size_hint_y:None
    GridLayout:
        cols:1
        size_hint_y:None
        height:self.minimum_height
        canvas:
            Color:
                rgb: 0, 0, 0, 0
            Rectangle:
                size: self.size

        Label:
            id: story
            font_size: '20sp'
            height: self.texture_size[1]
            size: self.texture_size
            text_size: self.width, self.height
            halign: "auto"
            valign: "center"
            pos_hint: "center_x": 0.7, "center_y": 0.49

            text: 

        """A boy and a girl were playing together. The boy had a collection of marbles. The girl has some 
        sweets with her. The boy told the girl that he would give her all his marbles in exchange for the 
        sweets with her. The girl agreed.

        The boy kept the most beautiful and the biggest marbles with him and gave her the remaining marbles. 
        The girl gave him all her sweets as she promised. That night the girl slept peacefully. But the boy 
        could not sleep as he kept wondering if the girl has hidden some sweets from him the way he had 
        hidden the best marbles from her.

        Moral of the Story :

        If you do not give 100 percent in a relationship, you will always kept doubting if the other person 
        has given her / his hundred percent. This is applicable for any relationship like love, employee – 
        employer, friendship, family, countries, etc…
        """
            ```

【讨论】:

现在它显示一个空白的白色屏幕,我的主屏幕规则是从 Screen 继承的【参考方案2】:

这是另一个答案,但这在ScrollView 中只有Label

<MainScreen>:
    BoxLayout:
        pos_hint: "center_x": 0.7, "center_y": 0.49
        size_hint_y: None
        height: 150  # make smaller than Label to force scrolling
        canvas:
            Color:
                rgb: 0, 0, 0, 0
            Rectangle:
                size: self.size

        ScrollView:
            do_scroll_x: False
            Label:
                id: story
                font_size: '20sp'
                size_hint_y: None
                height: self.texture_size[1]
                text_size: root.width, None
                halign: "auto"
                valign: "center"

                text:

                    """A boy and a girl were playing together. The boy had a collection of marbles. The girl has some
                    sweets with her. The boy told the girl that he would give her all his marbles in exchange for the
                    sweets with her. The girl agreed.

                    The boy kept the most beautiful and the biggest marbles with him and gave her the remaining marbles.
                    The girl gave him all her sweets as she promised. That night the girl slept peacefully. But the boy
                    could not sleep as he kept wondering if the girl has hidden some sweets from him the way he had
                    hidden the best marbles from her.

                    Moral of the Story :

                    If you do not give 100 percent in a relationship, you will always kept doubting if the other person
                    has given her / his hundred percent. This is applicable for any relationship like love, employee –
                    employer, friendship, family, countries, etc…
                    """

阅读ScrollView 文档中的important part。

【讨论】:

你能详细说明一下吗,因为我试过了,它在有文字的白色部分和底部的 1/4 黑色部分分割屏幕 3/4 没有看到您的其余代码,我无法确定发生了什么。但是,我确实在BoxLayout 中添加了size_hint_yheight,只是为了让ScrollView 足够小以强制它进行滚动。

以上是关于向下滚动屏幕以获取更长的文本,例如 kivy 中的故事的主要内容,如果未能解决你的问题,请参考以下文章

悬停时图像滚动

将文本片段扩展为更长的文本块

Python kivy 更新不同屏幕中的标签文本

excel4node lib自动调整excel单元格以适应更长的文本

使用Excel VBA调整列表框以显示比列表框宽度更长的字符串

如何在快速向上或向下滚动时退出键盘? [关闭]