Kivy按钮尺寸调整求助:如何缩小按钮并添加间距?
Kivy按钮尺寸调整及间距问题解决
问题核心原因
你写的size-hint是语法错误,KV语言中正确的属性名是**size_hint**(下划线分隔),这是修改无效的直接原因。
完整解决方案代码
要实现按钮缩小至原有尺寸的50%且保留间距,需要做以下调整:
- 修正
size_hint的拼写错误 - 给外层
BoxLayout添加spacing属性设置按钮间距 - 调整按钮内部
Image的位置绑定,避免偏移
修改后的KV代码:
#:import NoTransition kivy.uix.screenmanager.NoTransition <Predictor>: ScreenManager: transition: NoTransition() id: sm size: root.width, root.height Screen: name: 'homepage_screen' Image: source: 'images/homepage_background.png' allow_stretch: True keep_ratio: False BoxLayout: size_hint: 1, 0.10 spacing: 20 # 设置按钮间的间距,数值可按需调整 Button: size_hint: 0.5, 1 # 修正拼写,宽度占父布局50%,高度拉满 id: underOver_button_homepage on_press: root.underOver(img_underOver, sm) background_color: 0, 0, 0, 0 Image: id: img_underOver source: 'images/underover_button.png' allow_stretch: True keep_ratio: False size: self.parent.size pos: self.parent.pos # 绑定到按钮位置,避免图片偏移 Button: size_hint: 0.5, 1 # 修正拼写 id: side_button_homepage on_release: root.side(img_side, sm) background_color: 0, 0, 0, 0 Image: id: img_side source: 'images/side_button.png' allow_stretch: True keep_ratio: False size: self.parent.size pos: self.parent.pos
额外说明
- 如果希望按钮是原固定尺寸的50%(而非父布局占比),可以改用固定尺寸搭配
size_hint: None, None,示例:Button: size_hint: None, None size: 200 * 0.5, 80 * 0.5 # 替换为你原按钮的实际宽高数值 spacing的数值可根据界面视觉需求灵活调整,单位为像素。
内容的提问来源于stack exchange,提问作者user21099627
相关产品推荐
相关产品推荐

