Kivy,StackLayout,对象顺序 [英] Kivy, StackLayout, object order

查看:105
本文介绍了Kivy,StackLayout,对象顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有StackLayout的屏幕.堆栈的第一行包括一个textinput和一个"+"按钮,用于在循环中的实际一行下面添加另一行(即,另一个textinput和另一个"add"按钮).然后有一个保存"按钮,该按钮应该始终位于堆栈的末尾. 当按保存按钮时,字典本应稍后从文本字段中获取输入,但这与我的问题无关.

I have a screen with a StackLayout. The first row of the stack includes a textinput and a "+" button which is meant to add another identical row below the actual one in a loop (i.e. another textinput with another "add" button). Then there is a "Save" button, which is supposed to be always at the end of the stack. The dictionary is supposed to later grab the input from the text field, when pressing the save button, but this should not be relevant to my problem.

我的代码有两个问题:

  • 首先,当我连续按下"+"按钮时,突出显示的按钮不是该按钮本身,而是下面一行中的按钮(例如,如果有三行,则在第二行中按下按钮行,第三行中的按钮将突出显示)
  • 第二个也是最重要的一点,从第二行开始,每按一次"+"按钮都会在实际行中的上方而不是下方添加一个新行.
  • First, when I press the "+" button in a row, the button that gets highlighted is not this button itself, but the one in the row below (e.g. if there are three rows and I press the button in the second row, the button in the third row is highlighted)
  • Second and most importantly, starting from the second row onwards, each press of the "+" button adds a new row above the actual one, rather than below it.

我知道kivy为小部件分配了相反的顺序(即,最后添加的小部件将首先绘制),这就是为什么我要手动为新行分配索引的原因.但是,我无法实现所需的行为.

I am aware that kivy assigns reverse order to the widgets (i.e. the one added last will be drawn first) and that is why I am manually assigning indexes to the new rows. However, I cannot achieve the desired behavior.

这是最低工作版本:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import ScreenManager, Screen

class AddWindow(Screen):
    def __init__(self, **kwargs):
        super(AddWindow, self).__init__(**kwargs)

        self.grid = StackLayout()
        self.grid.pos_hint = {"x":0.05,"top":0.8}
        self.grid.size_hint = (0.9,None)
        self.add_widget(self.grid)

        self.i = 1
        self.n = 1
        self.inputs = {}
        self.ing1 = TextInput(size_hint=(0.9,'0.3sp'))
        self.grid.add_widget(self.ing1)
        self.inputs['0'] = 'ing1'

        self.addIng = Button(text="+", size_hint=(0.1,'0.3sp'))
        self.addIng.bind(on_press=self.addIngredient)
        self.grid.add_widget(self.addIng)

        self.doneButton = Button(text="Save")
        self.grid.add_widget(self.doneButton, index=0)

    def addIngredient (self, instance):
        self.ing = TextInput(size_hint=(0.9,'0.3sp'))
        self.inputs[self.i] = 'ing{}'.format(self.i+1)
        self.grid.add_widget(self.ing, index=self.n+1)

        self.addNext = Button(text="+", size_hint=(0.1,'0.3sp'))
        self.addNext.bind(on_press=self.addIngredient)
        self.grid.add_widget(self.addNext, index=self.n+2)
        self.i += 1
        self.n += 2
        print(self.inputs)        

WMan = ScreenManager() 
WMan.add_widget(AddWindow(name='add'))


class RecipApp(App):
    def build(self):
        return WMan

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

我想念什么?这是一个屏幕截图,可以使您更加清楚: 截图

What am I missing? Here is a screenshot for better clarity: Screenshot

推荐答案

这是一种蛮力方法,每次按下+`Button时,通过重建StackLayout来完成您想要的事情:

Here is a brute force method to do what you want by rebuilding the StackLayout each time a + `Button is pressed:

def addIngredient(self, instance):
    tmp_children_list = self.grid.children[:]
    self.grid.clear_widgets()
    for index in range(len(tmp_children_list)-1, -1, -1):
        child = tmp_children_list[index]
        self.grid.add_widget(child)
        if child == instance:
            # this is the pressed Button, so add new row after it
            self.n += 1
            self.ing = TextInput(size_hint=(0.9,'0.3sp'))
            self.ing.text = str(self.n)  # add text just for identification
            self.grid.add_widget(self.ing)
            self.addNext = Button(text="+", size_hint=(0.1,'0.3sp'))
            self.addNext.bind(on_press=self.addIngredient)
            self.grid.add_widget(self.addNext)

这篇关于Kivy,StackLayout,对象顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆