首页 > 解决方案 > Python / Kivy:如何更改列表创建的方向

问题描述

我创建了一个响应式小部件列表,但每个新项目都添加在底部。我阅读了文档,在互联网上查找了信息,但我没有找到任何东西,我最后的希望是你)

它看起来像这样:

v
v
v
|
|
|Element 5 
|
|Element 6
|
|Element 7
|
v
v
v

我希望每个新元素都创建在其他元素之上。

也就是说,像这样:

^
^
^
|
|
|Element 7 
|
|Element 6
|
|Element 5
|
|Element 4
|
|Element 3
|
|Element 2
|
^
^
^

简而言之,我需要一切都一样,但反过来。

这是我的代码:

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)


Builder.load_string('''
<Button@Button>:
    font_size: 15
    font_name: 'Verdana'

<Label@Label>:
    font_size: 15
    font_name: 'Verdana'

<TextInput@TextInput>:
    font_size: 15
    font_name: 'Verdana'
    padding_y: 3


<Row>:
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

    Button:
        text: 'World 1'
        width: 300

<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

<User>:
    BoxLayout:
        orientation: "vertical"
        padding : 20, 5


        BoxLayout:
            orientation: "horizontal"
            #padding : 10, 10
            spacing: 10, 10
            size: 450, 40
            size_hint: None, None

            Label:
                size_hint_x: .2
                text: "Test 1"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .8
                text: "Test 2"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'


        ScrollView:
            Rows:
                id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"
            padding : 10, 5
            spacing: 10, 10
            size_hint: .5, .35
            pos_hint: {'x': .25, 'y':.25}

            Button:
                text: 'Ok'

            Button:
                text: 'Cancel'
            ''')

class User(Screen):

    def add_more(self):
        self.ids.rows.add_row()


class Row(BoxLayout):
    button_text = StringProperty("")


class Rows(BoxLayout):
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))


class Test(App):

    def build(self):
        self.root = Builder.load_file('Demo.kv')
        return self.root


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

标签: pythonpython-3.xkivy

解决方案


add_widget()方法允许一个index参数,该参数指示新小部件应放置在子列表中的哪个位置。请参阅文档。所以你可以使用 thtindex参数来做你想做的事:

    self.add_widget(Row(button_text=str(self.row_count)), index=self.row_count-1)

推荐阅读