避免Kivy中动态调整大小导致的重叠

2024-03-29 13:06:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个GridLayout(这里称为CostosInput),当按下按钮时,它会将TextInputs添加到自身中。但是,当它这样做时,布局不会调整它周围的小部件的大小,这会导致它在添加一定量的TextInput小部件后重叠。我该怎么修?你知道吗

它是另一个GridLayout的一部分,它有一列,包含在ScrollView中。它下面的小部件(它与之重叠的那个)是一个Label。你知道吗

我试过在Kivy文档中查找,但我发现的唯一内容与LayoutsWidgets的大小有关,而不一定与它们的位置有关。我想它应该是layout_hint_with_bounds(sh_sum, available_space, min_bounded_size, sh_min_vals, sh_max_vals, hint)(link to the Kivy docs)之类的,但这是一个内部函数。如果有什么不符合惯例,我道歉。你知道吗

下面是CostoInput类的代码。你知道吗

class CostosInput(GridLayout):
def __init__(self, *args, **kwargs):
    super(CostosInput, self).__init__()
    self.cols = 2
    self.spacing = 5
    self.padding = 5
    self.size_hint_y = 0.2

    # used in add_lines()
    self.count = 1

    add = Orange_Button(text='AGREGAR COSTO')
    add.bind(on_press=self.add_lines)

    rem = Orange_Button(text='QUITAR COSTO')
    rem.bind(on_press=self.del_lines)

    self.add_widget(add)
    self.add_widget(rem)

    self.add_widget(Orange_Label(text='concepto'))
    self.add_widget(Orange_Label(text='costo'))

    self.add_lines()

# this function is adds TextInputs
# when the add button is pressed
def add_lines(self, *args, **kwargs):
    text = Normal_TI()
    text.text = 'costo ' + str(self.count)
    text.bind(text=self.change)
    flot = FloatInput()
    flot.bind(text=self.change)
    self.add_widget(text)
    self.add_widget(flot)
    self.size_hint_y += 0.1

    self.count += 1

# this has nothing to do with the ui
def del_lines(self, *args, **kwargs):
    for x in range(0, len(self.children) - 4, 2):
        if len(self.children) > 4:
            try:
                if self.children[x].focus or self.children[x + 1].focus:
                    self.remove_widget(self.children[x])
                    self.remove_widget(self.children[x])
            except Exception as err:
                print('del lines: no se pudieron quitar,'
                      'err {}'.format(err))

# this also has nothing to do with the ui
def change(self, value, *args):
    dicto = dict()
    for x in range(0, len(self.children) - 4, 2):
        dicto[self.children[x + 1].text] = self.children[x].text
    print(dicto)
    producto.costos = dicto

下面是标签实例化所用的类的代码。文本属性是唯一更改的部分。你知道吗

class Orange_Label(Label):
def __init__(self, *args, **kwargs):
    super(Orange_Label, self).__init__(*args, **kwargs)
    self.size_hint_y = None
    self.size = (self.width, 10)
    self.color = (0.95668627451, 0.6941176471, 0.5137254902, 1)

Tags: thetextselfaddsizeinitdefargs