当我在 kivy 中调整 window 大小时,如何防止我的标签拉伸?

How can i prevent my label from stretching when i resize the window in kivy?

在这个程序中,当我想调整 window 的大小时,我不希望我的标签调整大小或拉伸,我希望它具有固定的宽度我该怎么做? (我设置了 size_hint: (.1, None) 但我不知道为什么它会随着 window 大小的变化而延伸)

代码:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
 

Builder.load_string("""


<relative>
    RelativeLayout:
    
            
        size: root.width,root.height
        Label:
            pos_hint:{'x':0,'y':.01}
            size_hint: (.1, None)
            height: 22
            text:'1'
            background_color: 6/255, 61/255, 81/255, 1
            canvas.before:
                Color:
                    rgba:self.background_color
                Rectangle:
                    size: self.size
                    pos: self.pos

        TextInput:
        
            cursor_color: 255/255, 143/255, 5/255, 0.8 
            pos_hint:{'x':.1,'y':0}
            multiline:False
            height: 33
            size_hint: (5, None)
            background_color: 0,0,0,0
            foreground_color: 255/255, 167/255, 167/255, 0.51













""")

class relative(Widget):
    pass

class foo(App):

    def build(self):
        Window.clearcolor='#1618388'
        return relative()







if __name__ == '__main__':
    foo().run()

在更改 window 尺码之前:

更改 window 尺码后:

Label 改变了大小,因为 size_hint: (.1, None) 告诉 kivy Label 宽度应该是 RelativeLayout 的 0.1 倍。如果您希望大小不变,请不要使用 size_hint。试试这个:

    Label:
        pos_hint:{'x':0,'y':.01}
        size_hint: (None, None)
        width: 100
        height: 22
        text:'1'
        background_color: 6/255, 61/255, 81/255, 1
        canvas.before:
            Color:
                rgba:self.background_color
            Rectangle:
                size: self.size
                pos: self.pos