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

0

このように、Label をその中のテクスチャのサイズに設定できます。

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 に答える