Kivy:无法更改嵌套GridLayout的高度

时间:2019-08-07 02:59:09

标签: python python-3.x kivy height

我在kivy中有一个简单的用户界面,它由一个大的gridlayout内部的几个gridlayout组成。

代码
这是界面的kv代码

Builder.load_string('''
<GreyLabel>:
    size_hint_y: None
    height: 30
    canvas.before:
        Color:
            rgba: .7, .7, .7, 1
        Rectangle:
            pos: self.pos
            size: self.size

<ContainerBox>:
    orientation: 'horizontal'

    GridLayout:
        cols: 1
        row_force_default: True
        foo: [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
        ResizingFrame:
            id: Row1
            cols: 1
            GreyLabel:
                text: "Something goes here"
        GridLayout:
            id: Row2
            cols: 1
            Label:
                height: 30
                text: 'Label One'
                canvas.before:
                    Color:
                        rgba: .4, .4, .4, 1
                    Rectangle:
                        pos: self.pos
                        size: self.size
            TextInput:
                height: 30
                multiline: False
                write_tab: False
                hint_text: 'Insert one liner'

        GridLayout:
            id: Row3
            cols: 1
            Label:
                height: 45
                text: 'Label two'
            Button:
                text: 'Button One'
                height: 60
            GridLayout:
                rows: 1
                height: 25
                Button:
                    text: 'Button Two'
                Button:
                    text: 'Button three'
''')

注意ResizingFrame项目。这是一个GridLayout类,带有以下代码

class ResizingFrame(GridLayout):
    c_value = StringProperty('SomeThing goes here')

    def __init__(self, **kwargs):
        super(ResizingFrame, self).__init__(**kwargs)
        Clock.schedule_once(lambda dt: self.adjustHeight(), 1)

    def adjustHeight(self):
        print("ResizingFrame.adjustHeight() before:", self.height)
        self.height = 40
        print("ResizingFrame.adjustHeight() after:", self.height)

您可以找到完整的代码here

目标
我希望能够更改ResizingFrame的高度。 该界面开始如下所示: enter image description here

1秒后,它应如下所示: enter image description here 但这不是那样。

只要adjustHeight()运行,它就会说:
ResizingFrame.adjustHeight()之前:100
ResizingFrame.adjustHeight()之后:40

但是由于某种原因,这种高度变化实际上从未发生过。我根本无法更改ResizingFrame的高度。为什么会这样呢?我该如何解决?

注意
这与this question有关。

我怀疑问题与以下内容有关:
foo: [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
这应将行的最小高度设置为其子代的高度。但是正如您在屏幕截图1中所看到的那样,这没有发生。它只是将最小高度设置为100,之后便无法更改高度。我什至不能将高度增加到100以上。它只是卡住了。

1 个答案:

答案 0 :(得分:2)

您无法更改ResizingFrame的大小的原因是,其size_hint优先于任何大小更改。因此,将您的adjustHeight方法修改为:

def adjustHeight(self):
    print("ResizingFrame.adjustHeight() before:", self.height)
    self.size_hint_y = None
    self.height = 40
    print("ResizingFrame.adjustHeight() after:", self.height)

将允许height更改生效。

请注意,如果没有将size_hint_y设置为None,则您正在更改height属性,并打印更改后的值。但是一旦您的adjustHeight方法完成后,就会重新计算ResizingFrame高度并将其更改回100。您可以通过多次调用原始adjustHeight方法({{1之前的}}总是bck到100)。

相关问题