You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Kivy中无法强制GridLayout默认尺寸 仅按钮外元素更小如何实现

Kivy应用元素尺寸调整解决方案

核心问题说明

  1. 属性访问顺序错误:代码中在self.top_grid实例化之前就调用了self.top_grid.cols = 2,会直接触发属性不存在的报错
  2. 布局参数未生效:Kivy中控件默认开启size_hint自适应权重,此时给父布局设置的row_default_height、col_default_width不会生效,需要手动关闭子控件的对应尺寸权重
  3. 字体尺寸过大:现有Label、TextInput的字体设置为30,本身就会撑大控件尺寸

调整步骤

  • 调整实例化顺序,先创建self.top_grid再配置其属性
  • 调小布局的默认行高、列宽数值
  • 给Label、TextInput设置size_hint=(None, None),使其继承父布局的默认尺寸
  • 调小对应控件的字体大小,匹配缩小后的控件尺寸

修正后完整代码

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class MyGridLayout(GridLayout):
    def __init__(self,**kwargs):
        super(MyGridLayout,self).__init__(**kwargs)
        self.cols = 1       
        self.press_count = 0
        # 初始化top_grid时直接传入cols参数,同时调小默认宽高
        self.top_grid = GridLayout(
            cols=2,
            row_force_default=True,
            row_default_height=40,
            col_force_default=True,
            col_default_width=150
        )
        
        # 所有子控件关闭size_hint,同时调小字体
        self.top_grid.add_widget(Label(text="name: ", font_size=16, size_hint=(None, None)))
        self.name = TextInput(multiline=False, font_size=16, size_hint=(None, None))
        self.top_grid.add_widget(self.name)
        
        self.top_grid.add_widget(Label(text="age: ", font_size=16, size_hint=(None, None)))
        self.age = TextInput(multiline=False, font_size=16, size_hint=(None, None))
        self.top_grid.add_widget(self.age)
        
        self.top_grid.add_widget(Label(text="how are you feeling: ", font_size=16, size_hint=(None, None)))
        self.feel = TextInput(multiline=False, font_size=16, size_hint=(None, None))
        self.top_grid.add_widget(self.feel)
        
        self.add_widget(self.top_grid)
        # 按钮尺寸保留原有逻辑
        self.button = Button(text="go!!!",font_size=32,size_hint_y=None,height=(100-self.press_count))
        self.button.bind(on_press=self.press)
        self.add_widget(self.button)
        
    def press(self, instance):
        name = self.name.text
        age = self.age.text
        feel = self.feel.text
        # 提交后生成的Label也可调整字体大小适配
        self.add_widget(Label(text=f"you are {name} and you are {age} and you are feeling {feel}",font_size=14))
        
        self.name.text = ""
        self.age.text = ""
        self.feel.text = ""
        
        self.press_count +=10
        if not self.press_count == 20:
            self.button.height=(100-self.press_count)
            
            
class MyApp(App):
    def build(self):
        return MyGridLayout()
        
        
if __name__ == "__main__":
    MyApp().run()

参数调整说明

如果觉得尺寸还是不符合预期,可以自行修改以下参数:

  • row_default_height:控制top_grid内每行的高度,数值越小行高越小
  • col_default_width:控制top_grid内每列的宽度,数值越小列宽越小
  • 各个Label、TextInput的font_size:控制控件内文字大小,数值越小文字越小

内容的提问来源于stack exchange,提问作者thilina perera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 03:39:01