如何用Python和Kivy缩小或消除MDIconButton背景与内边距
问题描述
我编写了一段创建MDIconButton的简单Python代码如下:
icon = MDIconButton( icon = "close-circle", md_bg_color = "red", theme_icon_color = "Custom", icon_color = "white", pos_hint = {"top": 1, "right": 1}, icon_size = "18sp", )该按钮在目标Widget中尺寸过大,我尝试通过icon_size属性缩小它,但当图标尺寸调至一定值后,按钮背景会比图标本身更大。请问是否可以缩小甚至完全消除MDIconButton的背景及内边距?
解决方案
完全消除背景:将
md_bg_color设为transparent,直接去掉背景色:icon = MDIconButton( icon = "close-circle", md_bg_color = "transparent", theme_icon_color = "Custom", icon_color = "white", pos_hint = {"top": 1, "right": 1}, icon_size = "18sp", )移除/缩小内边距:MDIconButton默认带有内边距,通过
padding属性设置为0或自定义数值即可消除多余间距:icon = MDIconButton( icon = "close-circle", md_bg_color = "red", theme_icon_color = "Custom", icon_color = "white", pos_hint = {"top": 1, "right": 1}, icon_size = "18sp", padding = 0, )同时移除背景和内边距:结合上述两个属性,让按钮仅显示图标:
icon = MDIconButton( icon = "close-circle", md_bg_color = "transparent", theme_icon_color = "Custom", icon_color = "white", pos_hint = {"top": 1, "right": 1}, icon_size = "18sp", padding = 0, )保留背景但匹配图标尺寸:如果需要保留背景,可通过
radius属性调整背景大小,使其与图标尺寸一致(半径设为图标尺寸的一半):icon = MDIconButton( icon = "close-circle", md_bg_color = "red", theme_icon_color = "Custom", icon_color = "white", pos_hint = {"top": 1, "right": 1}, icon_size = "18sp", padding = 0, radius = "9sp", )
内容的提问来源于stack exchange,提问作者Timson
相关产品推荐
相关产品推荐

