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

Kivy中窗口大小变化时Label无法自适应,如何动态调整?

Kivy窗口调整时Label无法适配的解决方法

你遇到的问题是Label设置size_hint: (1, None)后,窗口缩小时文本超出显示范围,核心原因是未配置文本自动换行的属性,导致文本无法根据窗口宽度自适应调整。

解决方案

给Label添加文本换行与高度自适应的属性,修改后的代码如下:

<ForgotPasswordGridLayoutDesign>:
    cols:1
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1  # White color (RGBA format)
        Rectangle:
            pos: self.pos
            size: self.size
    Label:
        text: "Forgot Password"
        font_size: '50dp'
        color: .2,.5,.7,.7
        size_hint: (1, None)
        markup: True
        # 新增属性实现适配
        text_size: self.width, None  # 文本按Label宽度自动换行
        halign: 'center'  # 文本水平居中
        valign: 'middle'  # 文本垂直居中
        height: self.texture_size[1]  # 高度随文本内容自动调整
    TextInput:
        hint_text: "Enter username here"
        singleline:True
        size_hint: 1, None
        height: "30dp"
        spacing: 10
    Button:
        text: "Confirm"
        size_hint: .4,None
        height: "30dp"

关键属性说明

  • text_size: self.width, None:指定文本渲染的宽度范围为Label自身宽度,高度不限制,文本会自动换行适配宽度。
  • height: self.texture_size[1]:让Label高度等于文本渲染后的实际高度,避免文本被截断。
  • halign/valign:确保文本在Label内居中显示,保持视觉一致性。

内容的提问来源于stack exchange,提问作者Ery Pisalbon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 20:42:26