Kivy (KivyMD) 中的按钮和小部件尺寸

问题描述 投票:0回答:1

早上好, 我正在研究我的跨平台应用程序项目。我在 KivyMD 中的尺寸管理方面遇到了麻烦。 我想更改小部件的大小和位置,并且我想将

size_hint
设置为
(None, None)
,以便在跨平台设备上正确工作。我尝试使用
size
参数更改大小,但这仅适用于 y 轴。 X 轴仍处于起始位置。我想询问其他小部件,如 iconsFloatLayout 层,如何将
size_hint
设置为
None
,并更改大小以便在跨平台设备上正确工作。

#: import utils kivy.utils
MDScreen:
    name: "LoginPage"
    on_enter:
        app.animationBack1(back1)
        app.animationBack2(back2)
        app.animationIcon(userIcon)
        app.animationLoginLabel(loginLabel)

MDFloatLayout:
    MDFloatLayout:
        id: back1
        size_hint_y: .3
        pos_hint: {'center_y': 1.5}
        canvas.before:
            Color:
                rgb: utils.get_color_from_hex('#74A3FC')

            Rectangle:
                size: self.size
                pos: self.pos

    MDFloatLayout:
        id: back2
        size_hint_y: .6
        pos_hint: {'center_y': 1.5}
        canvas.before:
            Color:
                rgb: utils.get_color_from_hex('#74A3FC') # most
            Ellipse:
                size: self.size
                pos: self.pos

    MDIcon:
        id: userIcon
        halign: 'center'
        icon: 'account-circle'
        font_size: '70sp'
        pos_hint: {'center_x': .5, 'center_y': .75}
        opacity: 0 # visibility

    MDLabel:
        id: loginLabel
        text: '[size=60][b]Login Page[/b][/size]'
        markup: True
        halign: 'center'
        pos_hint: {'center_y': .8}
        opacity: 0 # visibility

    MDTextField:
        id: email
        hint_text: 'Enter Email'
        required: True
        helper_text_mode: 'on_error'
        helper_text: 'Please, enter Your Email.'
        color_mode: 'custom'
        line_color_focus: utils.get_color_from_hex('#74A3FC')
        current_hint_text_color: utils.get_color_from_hex('#74A3FC') # can be, otherwise after running app, hint_text will be grey
            # after error rasing hint_text will be also customized
        size: 800, 1
        size_hint: None, None
        pos_hint: {'center_x': .5, 'center_y': .45}

    MDTextField:
        id: password
        hint_text: 'Enter Password'
        password: True
        required: True
        helper_text_mode: 'on_error'
        helper_text: 'Please, enter Your Password.'
        pos_hint: {'center_x': .5, 'center_y': .3}
        size: 800, 1
        size_hint: (None, None)
        line_color_focus: utils.get_color_from_hex('#74A3FC')
        current_hint_text_color: utils.get_color_from_hex('#74A3FC')
        color_mode: 'custom' # must be, otherwise on the second click line color 
will customized

    MDFillRoundFlatButton:
        text: 'Login'
        pos_hint: {'center_x': .5, 'center_y': 0.05}
        size: 800, 100
        size_hint: (None, None) #text never leaves the button-I don't know why
        # theme_text_color: 'Error' #['Primary', 'Secondary', 'Hint', 'Error', 'Custom', 'ContrastParentBackground'] - only change text color
        md_bg_color: utils.get_color_from_hex('#74A3FC')
python kivy kivy-language kivymd
1个回答
0
投票

遇到同样的问题,花了很长时间在网上寻找答案。最后阅读 .../python3.10/site-packages/kivymd/uix/button/button.py 中的源代码,并使用 _min_width 和 _min_height 找到了这个解决方法(KivyMD 1.1.1):

MDFillRoundFlatButton:
        text: 'Login'
        pos_hint: {'center_x': .5, 'center_y': 0.05}
        md_bg_color: utils.get_color_from_hex('#74A3FC')
        _min_width:  800
        _min_height: 100
© www.soinside.com 2019 - 2024. All rights reserved.