0

我是新来的基维。我有两个屏幕,我想将它们合并为一个。

第一个屏幕的代码:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView    
from kivy.lang import Builder


Builder.load_string('''
<Button>:
    font_size: 40
    color: 0,1,0,1

<Widgets>:
    Button:
        size: root.width/2, 75
        pos: root.x , root.top - self.height
        text: 'OK'

    Button:
        size: root.width/2, 75
        pos: root.x + root.width/2, root.top - self.height
        text: 'Cancel'

    Label:
        text: "bbbbbaäa üß AäÄ"
        size: root.width, 75
        valign: 'middle'
        halign: 'center'
        anchor: 'left'
        pos: root.x, root.top - 150
        font_size: 50
        height: 75


    Label:
        text: "Tssssssssssa #aaäa Äaaäa Üaa Maaäa a"
        size: root.width, 75
        valign: 'middle'
        halign: 'left'
        anchor: 'left'
        pos: root.x, root.top - 150 - 50
        font_size: 30
        height: 50

''')



class ScrollableLabel(ScrollView):
    pass


class Widgets(Widget):
    def build(self):
        return Widgets()


class MyApp(App):
    def build(self):
        return Widgets()


if __name__ == "__main__":
    MyApp().run()

第二个屏幕的代码来自 Alexander Taylor:https ://github.com/kivy/kivy/wiki/Scrollable-Label

我在这里再次粘贴代码:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.lang import Builder

long_text = 'yay moo cow foo bar moo baa ' * 100

Builder.load_string('''
<ScrollableLabel>:
    Label:
        size_hint_y: None
        height: self.texture_size[1]
        text_size: self.width, None
        text: root.text
''')

class ScrollableLabel(ScrollView):
    text = StringProperty('')

class ScrollApp(App):
    def build(self):
        return ScrollableLabel(text=long_text)

if __name__ == "__main__":
    ScrollApp().run()

问题一:

我想将这两个屏幕组合成一个屏幕,Scrollbale Label使第二个屏幕位于Label第一个屏幕下方。我该怎么做?

问题二:

我想让第一个代码中的标签文本从左边开始。现在文本位于标签的中间。似乎这anchor: 'left'不起作用。我该怎么做?

帮助将不胜感激。谢谢。

4

1 回答 1

1

你可以这样做:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.lang import Builder
from kivy.properties import StringProperty


Builder.load_string('''
<Button>:
    font_size: 40
    color: 0,1,0,1

<Widgets>:
    GridLayout:
        size_hint: 1,0.4
        cols: 2
        rows: 2

        Button:
            text: 'OK'

        Button:
            text: 'Cancel'

        Label:
            text: "bbbbb"
            font_size: 50
            halign: "left"
            size: self.texture_size
            text_size: root.width/2, None

        Label:
            text: "Tssssssssssa #aaäa Äaaäa Üaa Maaäa a"
            font_size: 30
            halign: "left"
            size: self.texture_size
            text_size: root.width/2, None

    ScrollView:
        Label:
            size_hint_y: None
            height: self.texture_size[1]
            text_size: self.width, None
            text: root.text

''')



class Widgets(BoxLayout):
    text = StringProperty("")
    def __init__(self,**kwargs):
        super(Widgets,self).__init__(**kwargs)
        self.orientation = "vertical"
        self.text = 'yay moo cow foo bar moo baa ' * 100



class MyApp(App):
    def build(self):
        return Widgets()



if __name__ == "__main__":
    MyApp().run()
于 2016-08-14T19:31:15.420 回答