Kivy - как изменить размер элементов внутри сетки? - PullRequest
0 голосов
/ 12 июня 2018

Я новичок в Kivy Framework и пытаюсь использовать GridLayout для создания трех столбцов.Внутри 3-го столбца я хочу изменить ширину элемента, чтобы она была меньше и выровнена по правому краю (я не хочу изменять всю ширину столбца), однако мои попытки не работают.

main.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()

test.kv

<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
            width: 10

enter image description here

Ответы [ 2 ]

0 голосов
/ 12 июня 2018

Решение состоит в том, чтобы установить AnchorLayout в третьем столбце и внутри этого макета кнопку:

<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

Для лучшей наглядности давайте разместим цвета фона:

<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'

            canvas.before:
                Color:
                    rgba: 1, 0, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size

        TextInput:
            id: text_field
            multiline: False

        AnchorLayout:
            anchor_x: 'right'
            canvas.before:
                Color:
                    rgba: 0, 0, 1, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
            Button:
                id: f_but
                width: 40
                size_hint_x: None

enter image description here

0 голосов
/ 12 июня 2018

try size_hint_x: -0.5

вместо свойства width в вашем 3-м элементе столбца.

Используя GridLayout , вы можете использовать size_hint_x, чтобы зафиксировать высоту столбца.до определенного размера.

применяя изменения:

test.kv

<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

Надеюсь, эта помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...