StackLayout中的对象顺序(Kivy)

时间:2016-09-23 19:59:46

标签: python layout kivy

我有一个

layout = StackLayout()

现在我按下这样的按钮

for x in range(9): # range() explanation: http://pythoncentral.io/pythons-range-function-explained/
            bt = Button(text=' ', font_size=200, width=200, height=200, size_hint=(None, None), id=str(x+1))
            bt.bind(on_release=self.btn_pressed)
            layout.add_widget(bt)

问题在于,当我浏览layout.children数组并显示id时,它们按顺序9,8,7,6,5,4,3,2,1进行,这是颠倒而不是我想要的。如何按正确顺序排列? (反转阵列不是解决方案)

另一个例子

我有:

        self.layout = StackLayout()
        bt = Button(text='1', font_size=200, width=200, height=200, size_hint=(None, None), id="1")
        bt1 = Button(text='2', font_size=200, width=200, height=200, size_hint=(None, None), id="2")
        bt2 = Button(text='3', font_size=200, width=200, height=200, size_hint=(None, None), id="3")
        bt3 = Button(text='4', font_size=200, width=200, height=200, size_hint=(None, None), id="4")
        bt4 = Button(text='5', font_size=200, width=200, height=200, size_hint=(None, None), id="5")
        bt5 = Button(text='6', font_size=200, width=200, height=200, size_hint=(None, None), id="6")
        self.layout.add_widget(bt, 1)
        self.layout.add_widget(bt1, 2)
        self.layout.add_widget(bt2, 3)
        self.layout.add_widget(bt3, 4)
        self.layout.add_widget(bt4, 5)
        self.layout.add_widget(bt5, 6)

这告诉我:enter image description here

2 个答案:

答案 0 :(得分:2)

Kivy在此处撤销订单,原因与内部调度订单有关。它不是必须的,但它是一个设计决定。

但是,这根本不重要。如果您想将对象存储在某个特定结构中,请自己动手。

答案 1 :(得分:0)

可能的伎俩是:

y = 10

for x in range(y):
        bt = Button(text=' ', font_size=200, width=200, height=200, size_hint=(None, None), id=str(y-x))
        bt.bind(on_release=self.btn_pressed)
        layout.add_widget(bt)