Kivy (KivyMD) 中的按钮和小部件大小
Posted
技术标签:
【中文标题】Kivy (KivyMD) 中的按钮和小部件大小【英文标题】:Button and Widget size in Kivy (KivyMD) 【发布时间】:2021-06-08 06:19:18 【问题描述】:早上好,
我从事我的跨平台应用程序项目。我在 KivyMD 中遇到了尺寸管理问题。
我想更改我的小部件的大小和位置以及我想将size_hint
设置为(None, None)
以便在跨平台设备上正常工作。我尝试使用 size
参数更改大小,但这仅适用于 y 轴。 X 轴仍处于起始位置。我想询问其他小部件,如 icons、FloatLayout 层如何将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')
【问题讨论】:
【参考方案1】:尝试使用
root.width*i, root.height*j ##when dealing with widget size and pos it will get u what u want
【讨论】:
以上是关于Kivy (KivyMD) 中的按钮和小部件大小的主要内容,如果未能解决你的问题,请参考以下文章