如何改变网格布局内元素的大小?

2024-10-02 22:32:01 发布

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

我是kivy框架的新手,正在尝试使用GridLayout创建三个列。在第3列中,我希望将元素的宽度更改为更小并向右对齐(我不想更改整个列宽),但是我的尝试没有成功。在

在主.py在

from kivy.app import App
from kivy.uix.widget import Widget

class AppCore(Widget):
    pass

class TestApp(App):
    def build(self):
        return AppCore()

def run_app():
    TestApp().run()

if __name__ == '__main__':
    run_app()

在试验电压在

^{pr2}$

enter image description here


Tags: runfromimport框架app元素defwidget
2条回答

解决方案是在第三列中建立一个^{},并在该布局中设置按钮:

<AppCore>
    GridLayout:
        cols: 3
        size: root.width * 0.8, root.height * 0.8
        row_default_height: 30
        row_force_default: True
        center: root.width / 2, root.height / 2

        Label:
            text: 'hello world'

        TextInput:
            id: text_field
            multiline: False

        AnchorLayout:
            anchor_x: 'right'
            Button:
                id: f_but
                width: 40
                size_hint_x: None

enter image description here

为了更好地显示,让我们放置背景色:

^{pr2}$

enter image description here

尝试size_hint_x: -0.5

而不是第三个column元素中的width属性。在

使用GridLayout,可以使用size\u hint_x将列高度固定为特定大小。在

应用更改:

在试验电压在

<AppCore>
    GridLayout:
        cols: 3
        size: root.width * 0.8, root.height * 0.8

        row_default_height: 30
        row_force_default: True
        center: root.width / 2, root.height / 2

        Label:
            text: 'hello world'

        TextInput:
            id: text_field
            multiline: False

        Button:
            id: f_but
            padding_right: 0
            size_hint_x: -0.5

希望这能有所帮助。在

相关问题 更多 >