1

我无法让 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()
4

2 回答 2

1

Labela (and )的默认大小Widget是 (100,100)。您是否看到屏幕上的所有文本都没有关系。如果您打印myLabel.size,您将意识到这一点。您需要确保设置height标签 ( myLabel.height: 2200) 的 ,但首先将 设置size_hint_yNone第一个 ( 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()
于 2013-07-01T23:41:33.020 回答
0

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

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]
于 2013-07-02T17:26:41.370 回答