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

2024-06-09 16:08:12 发布

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

我在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()运行时,它就会说:
调整框架大小。调整高度()之前:100
调整框架大小。调整高度()之后:40

但由于某种原因,这种高度变化实际上从未发生过。我根本无法改变ResizingFrame的高度。为什么会这样?我该怎么修?你知道吗

注意
这与this question有关。你知道吗

我怀疑问题与线路有关:
foo: [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
这应该将行的最小高度设置为其子行的高度。但正如你在截图1中看到的,这并没有发生。它只是把最小高度设为100,然后我就不能再改变高度了。我甚至不能把身高提高到100以上。只是卡住了。你知道吗


Tags: 代码textposselfsize高度buttonlabel
1条回答
网友
1楼 · 发布于 2024-06-09 16:08:12

无法更改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方法看到这一点(beforeheight始终是bck to 100)。你知道吗

相关问题 更多 >