标签未正确对齐

Label not aligned correctly

我在 eclipse luna 上使用 pydev。我的kv文件如下:

<LoginForm>:
    userid: userid
    password: password

    size_hint_x: 0.5
    size_hint_y: None
    height: 200

    orientation: 'vertical'
    pos_hint: {'center_x': 0.5,'center_y':0.5}

    minimum_height: 100
    minimum_width: 100

    #User ID
    Label:
        text: 'User ID'
        font_size: 20
        size_hint_x: None

    TextInput:
        id: userid
        font_size: 20


    #User PW
    Label:
        text: 'Password'
        font_size: 20

    TextInput:
        id: password
        password: True
        font_size: 20

    Button:
        text: 'Login'

我的python代码是:

from kivy.app import App;
from forms.login import LoginForm;
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout

class LoginForm(BoxLayout):

    def __init__(self, **kwargs):
        super(LoginForm, self).__init__(**kwargs)

class StartApp(App):
    def build(self):
        Window.size = (480, 800)
        return LoginForm()

        #return StartApp();

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

输出:

代码工作正常,但是,我的问题是左侧仍有一些间隙,其他控件不存在。我希望用户 ID 完全左对齐(在上图中它是左对齐的,但仍然有一些 space)。

你能advice/correct告诉我哪里出错了吗?

Label 没有左对齐,因为您实际上没有设置它,通过禁用 size_hint_x 它只采用 100 像素的默认宽度,文本显示在其中心。

声明标签有两种选择。

Label:
        text: 'User ID'
        font_size: 20
        size_hint_x: None
        width: self.texture_size[0]

这会将标签的宽度设置为包含文本图像的纹理的精确大小。但是,我认为执行以下操作可能更可取:

Label:
        text: 'User ID'
        font_size: 20
        text_size: self.size
        halign: 'left'
        valign: 'middle'

这样,您可以设置 text_size(它控制文本纹理的边界框)而不是弄乱小部件 size/position,而内置的文本对齐选项会处理其余部分.

在这种情况下,这些结果即使不相同也应该是相似的。