在猕猴桃中水平对齐按钮

时间:2020-08-18 11:55:25

标签: python kivy

This is how my program looks right now.

如何将我的按钮“登录”始终与 居中 保持 水平 对齐?我曾尝试在Stack Overflow上查找类似内容,但对我没有任何帮助...如果有人可以帮助我,我将非常感激。

这是我的python代码:

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
import kivy.properties as kyprops
from kivy.uix.widget import Widget
from kivy.uix.image import Image

Builder.load_file("main.kv")
Window.clearcolor = (1,1,1,1)
# Declare both screens
class LoginScreen(Screen):
    #txt_inpt = kyprops.ObjectProperty(None)
    #def __init__(self):
    pass

class InfoScreen(Screen):
    pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(LoginScreen(name='Login'))
sm.add_widget(InfoScreen(name='Info'))

class TestApp(App):
    def build(self):
        return sm

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

这是我的.kv代码:

#:import C kivy.utils.get_color_from_hex

<LoginScreen>:

    RelativeLayout:

        txt_inpt: txt_inpt

        canvas:
            Color:
                rgba: C('#336699')
            Line:
                width: 2
                rectangle: (0,self.height-50,self.width,0)
                id: linija
            Ellipse:
                pos: self.width- 35, self.height-15
                size: 7 , 7.0000000001
                angle_start: 0
                angle_end: 360
            Ellipse:
                pos: self.width- 35, self.height-27
                size: 7 , 7.0000000001
                angle_start: 0
                angle_end: 360
            Ellipse:
                pos: self.width- 35, self.height-39
                size: 7 , 7.0000000001
                angle_start: 0
                angle_end: 360

        TextInput:
            id: txt_inpt
            password: True
            multiline: False
            hint_text: 'Username'
            hint_text_color:  C('#b3ccff')
            size_hint_x: 0.9
            size_hint_y: None
            pos_hint: {"x":0.05, "top":0.8}
            background_color: (1,1,1,0.2)
            on_text_validate: root.manager.current = 'Info'
            height: 30
            pos: 300,60

        TextInput:
            id: txt_inpt
            password: True
            multiline: False
            hint_text: 'Password'
            hint_text_color:  C('#b3ccff')
            size_hint_x: 0.9
            size_hint_y: None
            pos_hint: {"x":0.05, "top":0.7}
            background_color: (1,1,1,0.2)
            on_text_validate: root.manager.current = 'Info'
            height: 30
            pos: 300,60

        BoxLayout:

            width: 1
            pos: self.parent.pos  # important!
            orientation: 'vertical'
            halign: 'center'

            Button:
                on_press: root.manager.current = 'Info'
                background_color: C('#336699')
                pos: self.pos
                size: self.size
                size: 80,30
                size_hint: None, None
                ## NOTE: pos_hint: {"x":0.45, "top":0.6}
                font_name: 'Droid.otf'
                text: 'Log in'

<InfoScreen>:
    BoxLayout:
        Button:
            text: 'My settings button'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'Login'

1 个答案:

答案 0 :(得分:1)

您在kv中的行:

pos: self.parent.pos  # important!

BoxLayout放在RelativeLayout的左下角

您可以使用pos_hintminimum_width将其居中:

    BoxLayout:
        # width: 1
        # pos: self.parent.pos  # important!
        pos_hint: {'center_x':0.5, 'y':0}
        size_hint_x: None
        width: self.minimum_width
        orientation: 'vertical'
相关问题