0

Я создал адаптивный список виджетов но каждый новый элемент добавляется в низ.

Это выглядит так:

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()

1 ответ 1

0

add_widget()Метод позволяет для indexаргумента , который указывает , где в списке детей , что новый виджет должен быть помещен. См. Документацию . Итак, вы можете просто использовать этот indexаргумент, чтобы делать то, что хотите:

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

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.