Kivy ScrollView 用于文本段落

Posted

技术标签:

【中文标题】Kivy ScrollView 用于文本段落【英文标题】:Kivy ScrollView for paragraphs of text 【发布时间】:2013-06-29 04:00:19 【问题描述】:

我无法让 Kivy 中的 ScrollView 滚动浏览文本段落。我在下面附上了一个代码示例。谁能说明什么是错的?谢谢。

import kivy
import string

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView

class longTextLabelApp(App):

    def build(self):

        scrollViewLayout = ScrollView(do_scroll_x=False)
        childLayout = GridLayout(cols = 1, size_hint_x = 1, size_hint_y = None)
        childLayout.bind(minimum_height=childLayout.setter('height'))

        def longTextLabel():
            _long_text = """
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus odio nisi, pellentesque molestie adipiscing vitae, aliquam at tellus. Fusce quis est ornare erat pulvinar elementum ut sed felis. Donec vel neque mauris. In sit amet nunc sit amet diam dapibus lacinia. In sodales placerat mauris, ut euismod augue laoreet at. Integer in neque non odio fermentum volutpat nec nec nulla. Donec et risus non mi viverra posuere. Phasellus cursus augue purus, eget volutpat leo. Phasellus sed dui vitae ipsum mattis facilisis vehicula eu justo.

            Quisque neque dolor, egestas sed venenatis eget, porta id ipsum. Ut faucibus, massa vitae imperdiet rutrum, sem dolor rhoncus magna, non lacinia nulla risus non dui. Nulla sit amet risus orci. Nunc libero justo, interdum eu pulvinar vel, pulvinar et lectus. Phasellus sed luctus diam. Pellentesque non feugiat dolor. Cras at dolor velit, gravida congue velit. Aliquam erat volutpat. Nullam eu nunc dui, quis sagittis dolor. Ut nec dui eget odio pulvinar placerat. Pellentesque mi metus, tristique et placerat ac, pulvinar vel quam. Nam blandit magna a urna imperdiet molestie. Nullam ut nisi eget enim laoreet sodales sit amet a felis.
            """
            reallyLongText = _long_text + _long_text + _long_text + _long_text +_long_text
            myLabel = Label(text = reallyLongText, text_size = (700,None), line_height=1.5)
            return myLabel

        childLayout.add_widget(longTextLabel())
        scrollViewLayout.add_widget(childLayout)
        return scrollViewLayout

if __name__ == '__main__':
    longTextLabelApp().run()

【问题讨论】:

【参考方案1】:

Label(和Widget)的默认大小为 (100,100)。您是否看到屏幕上的所有文本都没有关系。如果您打印myLabel.size,您将意识到这一点。您需要确保设置标签的height (myLabel.height: 2200) 但首先将size_hint_y 设置为None (myLabel.size_hint_y=None)。以下代码应该可以工作:

import kivy
import string

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Rectangle, Color

class longTextLabelApp(App):

    def build(self):

        scrollViewLayout = ScrollView(do_scroll_x=False)
        childLayout = GridLayout(cols = 1, size_hint_x = 1, size_hint_y = None)
        childLayout.bind(minimum_height=childLayout.setter('height'))

        def longTextLabel():
            _long_text = """
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus odio nisi, pellentesque molestie adipiscing vitae, aliquam at tellus. Fusce quis est ornare erat pulvinar elementum ut sed felis. Donec vel neque mauris. In sit amet nunc sit amet diam dapibus lacinia. In sodales placerat mauris, ut euismod augue laoreet at. Integer in neque non odio fermentum volutpat nec nec nulla. Donec et risus non mi viverra posuere. Phasellus cursus augue purus, eget volutpat leo. Phasellus sed dui vitae ipsum mattis facilisis vehicula eu justo.

            Quisque neque dolor, egestas sed venenatis eget, porta id ipsum. Ut faucibus, massa vitae imperdiet rutrum, sem dolor rhoncus magna, non lacinia nulla risus non dui. Nulla sit amet risus orci. Nunc libero justo, interdum eu pulvinar vel, pulvinar et lectus. Phasellus sed luctus diam. Pellentesque non feugiat dolor. Cras at dolor velit, gravida congue velit. Aliquam erat volutpat. Nullam eu nunc dui, quis sagittis dolor. Ut nec dui eget odio pulvinar placerat. Pellentesque mi metus, tristique et placerat ac, pulvinar vel quam. Nam blandit magna a urna imperdiet molestie. Nullam ut nisi eget enim laoreet sodales sit amet a felis.
            """
            reallyLongText = _long_text + _long_text + _long_text + _long_text +_long_text
            myLabel = Label(text = reallyLongText, text_size = (700,None), line_height=1.5)

            print "The label size is ", myLabel.size    
            myLabel.size_hint_y = None
            myLabel.height = 2200

            return myLabel

        childLayout.add_widget(longTextLabel())
        scrollViewLayout.add_widget(childLayout)
        return scrollViewLayout

if __name__ == '__main__':
    longTextLabelApp().run()

编辑 - RST 文档

根据您的目标,使用 RSTDocument 可能会更好。标签就是这样,标签。他们不适应内容或文本。把它们想象成贴纸。 (RSTDocment)[http://kivy.org/docs/api-kivy.uix.rst.html],仍然显示为高度实验性,但更适合长文本,特别是如果它们是动态的。它们实际上包括卷轴。

from kivy.app import App
from kivy.uix.rst import RstDocument

class RstDocumentApp(App):

    def build(self):

        _long_text = """
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus odio nisi, pellentesque molestie adipiscing vitae, aliquam at tellus. Fusce quis est ornare erat pulvinar elementum ut sed felis. Donec vel neque mauris. In sit amet nunc sit amet diam dapibus lacinia. In sodales placerat mauris, ut euismod augue laoreet at. Integer in neque non odio fermentum volutpat nec nec nulla. Donec et risus non mi viverra posuere. Phasellus cursus augue purus, eget volutpat leo. Phasellus sed dui vitae ipsum mattis facilisis vehicula eu justo.

        Quisque neque dolor, egestas sed venenatis eget, porta id ipsum. Ut faucibus, massa vitae imperdiet rutrum, sem dolor rhoncus magna, non lacinia nulla risus non dui. Nulla sit amet risus orci. Nunc libero justo, interdum eu pulvinar vel, pulvinar et lectus. Phasellus sed luctus diam. Pellentesque non feugiat dolor. Cras at dolor velit, gravida congue velit. Aliquam erat volutpat. Nullam eu nunc dui, quis sagittis dolor. Ut nec dui eget odio pulvinar placerat. Pellentesque mi metus, tristique et placerat ac, pulvinar vel quam. Nam blandit magna a urna imperdiet molestie. Nullam ut nisi eget enim laoreet sodales sit amet a felis.
        """

        reallyLongText = _long_text + _long_text + _long_text + _long_text +_long_text
        return RstDocument(text = reallyLongText)

if __name__ == '__main__':
    RstDocumentApp().run()

【讨论】:

非常感谢您的回答。请问2200哪里来的?我对 size、size_hint、text_size 等感到困惑。 实际上 2000 效果更好,但我确实尝试并出错。我不认为标签应该处理大的动态文本。它们只是,好吧,标签:)。我可以建议您改用RstDocument。它包括它的卷轴。我将添加一个示例。如果您认为我回答了您的问题,请查看我的帖子作为答案,我将不胜感激。 (关于)[***.com/about] 你确实回答了 =)。我问这个问题只是一个例子。但我想这对我来说不太有效(除非我弄错了?)我需要提前知道正在滚动的对象的大小。对于使用什么来获得某种类型的布局的滚动效果有什么建议吗?在我的例子中,我想要一个包含文本和图像的布局,并且我不能上下滚动这个布局,这样我就可以查看所有的文本和图像。但是,这个布局和它的孩子是现场生成的,所以我不会提前知道它们的大小。有什么建议吗? 当您使用布局时,属性size_hint 是首选。 size_hint 具有相对于视觉空间从 0 到 1 的值(滚动时并非所有都是视觉空间)。首先,确保您的布局和内部小部件(标签和图像)获得了属性size_hint_y=None 用于垂直滚动。其次,手动设置小部件(标签和图像)的高度。如果您将 GridLayout 的 cols 属性设置为 1 并添加具有固定 height 的小部件,则滚动应该与您在 minimun_height 中所做的绑定正常工作 仔细查看(kivy 示例)[kivy.org/docs/….正如我所描述的,按钮循环具有固定的高度。如果仍然不清楚,您可能希望作为相关问题。我在 cmets 中没有足够的空间给你一个带有图像和文本的例子,但它应该是一样的。祝你好运。【参考方案2】:

您可以像这样将标签设置为内部纹理的大小。

FloatLayout:
    Label:
        # adding a background to see the amount of space the label takes
    canvas.before:
            Color:
                rgba: .5, .5, .5, .5
            Rectangle:
                size: self.size
                pos: self.pos
        text: "How can the only thing constant in the universe be `change` when\nchange itself is by it's very nature `not constant`?"
        pos_hint: 'center_x': .5, 'center_y': .5
        size_hint: None, None
        size: self.texture_size

但是,这样您只会得到一个标签,它只会扩展其中的文本量,并且需要您自己添加 \n 以使其换行。更好的方法是通过设置text_size 来让标签内的文本以一定的宽度自动换行,如下所示::

FloatLayout:
    ScrollView:
        # take half of parents size
        size_hint: .5, .5
        pos_hint: 'center_x': .5, 'center_y': .5
        Label:
            canvas.before:
                Color:
                    rgba: .5, .5, .5, .5
                Rectangle:
                    size: self.size
                    pos: self.pos
            text: "\n1. Two hunters are out in the woods when one of them collapses. He doesn't seem to be breathing and his eyes are glazed. The other guy whips out his phone and calls the emergency services. He gasps, `My friend is dead! What can I do?`\n\n The operator says `Calm down. I can help. First, let's make sure he's dead.`\n\n There is a silence, then a gun shot is heard. Back on the phone, the guy says `OK, now what?`\n\n\n2. Sherlock Holmes and Dr Watson were going camping. They pitched their tent under the stars and went to sleep. Sometime in the middle of the night Holmes woke Watson up and said:\n\n `Watson, look up at the sky, and tell me what you see.`\n\n Watson replied: `I see millions and millions of stars.`\n\n Holmes said: `And what do you deduce from that?`\n\n Watson replied: `Well, if there are millions of stars, and if even a few of those have planets, it’s quite likely there are some planets like Earth out there. And if there are a few planets like Earth out there, there might also be life.`\n\n And Holmes said: `Watson, you idiot, it means that somebody stole our tent.`\n\n\n3. Three women talk about their husband is performance as lovers.\n\nThe first woman says, `My husband is a marriage counselor, so he always buys me flowers and candy before we make love.`\n\nThe second woman says, `My husband is a motorcycle mechanic. He likes to play rough and use leather sometimes.`\n\nThe third woman shakes her head and says, `My husband works for an Internet company. He just sits on the edge of the bed and tells me how great it's going to be when I get it.` \n\n\n4. As within, so outside. Fractals equations show the possibility of having infinity within minutia. Each and every cell can be a image of the whole; merging the micro and the macro into one.\n"
            pos_hint: 'center_x': .5, 'center_y': .5
            size_hint: 1, None
            text_size: self.width, None
            height: self.texture_size[1]

【讨论】:

以上是关于Kivy ScrollView 用于文本段落的主要内容,如果未能解决你的问题,请参考以下文章

在kivy中将文本与ScrollView中的标签边缘对齐

Kivy Scrollview 自动滚动到新文本。防止向上滚动

Kivy Scrollview 自动滚动到新文本和行消失

TextInput中的kivy ScrollView实现

标签的 Kivy ScrollView 不会滚动

Kivy:如何从自定义 BoxLayout 显示 ScrollView?