python - 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()
解决方案
该add_widget()
方法允许一个index
参数,该参数指示新小部件应放置在子列表中的哪个位置。请参阅文档。所以你可以使用 thtindex
参数来做你想做的事:
self.add_widget(Row(button_text=str(self.row_count)), index=self.row_count-1)
推荐阅读
- angular - Angular 2 route resolver not working, browser blank and no errors in console
- discord.js - 从“quick.db”导入*作为数据库;= SyntaxError: Unexpected token * 我真的不知道这意味着什么或如何解决它?
- python - KNN 模型的准确度得分(IRIS 数据)
- swift - getting unresolved identifier 'self' in SwiftUI code trying to use Timer.scheduledTimer
- dialogflow-es - Google DialogFlow:查询 Firebase 数据库的策略/设计
- python - 为什么这个自定义记录器不将信息记录到控制台和文件?
- tomcat - Tomcat error - syntax error near unexpected token `('
- php - Yii2 - PHPSpreadSheet - 标头已发送
- linux - How do i allocate an array of bytes starting at address 0 in the LLVM IR?
- linux - 为什么相同的语言环境但一个有错误
我有相同的源来运行STAG和PROD,当复制一个日文名称的文件时,STAG出错并且PROD是no,为什么?
<java.nio.file.InvalidPathExceptio