簡體   English   中英

Kivy錨和網格布局居中

[英]Kivy Anchor and gridlayout centering

我正在使用錨點布局來容納小部件,我的小部件是gridlayout,問題是,我想要一個緊湊的登錄顯示UI,但是,我附近有3個控件,並且最后有注銷按鈕,所有按鈕都不在一起(不應該有間距),並且應在“錨點布局”中居中。

我得到它,因為它看起來不太好: 在此處輸入圖片說明

代碼如下:

.py:

from kivy.app import App
from formcontrol import FormControl

class MyApp(App):

    def build(self):
        self.title="sample App"
        self.formcontrol = FormControl()
        return self.formcontrol

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

FormControl.py:

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from login.logincodes import LoginControl
from login.logincodes import AfterLogin

class FormControl(AnchorLayout):
    '''
    classdocs
    '''

    def __init__(self, **kwargs):
        '''
        Constructor
        '''
        super(FormControl,self).__init__(**kwargs)
        c= LoginControl()
        c.setparent(self)
        self.add_widget(c)

    def changewidget(self,to):
        if to == 'AfterLogin':
            self.clear_widgets()
            self.add_widget(AfterLogin())

logincodes.py:

from kivy.uix.gridlayout import GridLayout


class LoginControl(GridLayout):
    _parentwidget=None

    def __init__(self, **kwargs):
        super(LoginControl,self).__init__(**kwargs)
        self.userid.text='Welcome'

    def setparent(self,parent):
        self._parentwidget=parent

    def changewidget(self,to):
        self._parentwidget.changewidget(to)

    def login_pressed(self,button):
        print(button.text)
        print(self.userid.text)
        print(self.userpw.text)
        self.changewidget('AfterLogin')

    def close_pressed(self,button):
        print(button.text)
        exit()


class AfterLogin(GridLayout):

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

my.ky文件:

<LoginControl>:
    rows: 3
    cols: 1

    userid: userid
    userpw: userpw

    BoxLayout:
        size_hint_y: None
        height: '32dp'
        Label:
            text: 'User ID'

        TextInput:
            id: userid
            text: 'some password'

    BoxLayout:
        size_hint_y: None
        height: '32dp'
        Label:
            text: 'Password'

        TextInput:
            id: userpw
            password: True
            text: 'some password'

    Button:
        text: 'Login'
        on_press: root.login_pressed(self)
        size_hint_y: None
        height: '32dp'

    Button:
        text: 'Close'
        on_press: root.close_pressed(self)
        size_hint_y: None
        height: '32dp'



<AfterLogin>:
    rows: 1
    Label:
        text: 'Logged In'

請讓我了解在配置UI時出錯的地方,並且我不想使用浮動布局,就像使用點放置小部件一樣。 我是Python和Kivy的新手。 您的支持和建議對我來說非常重要,可以幫助您改善,

GridLayout您指定rows = 3和cols = 1,這意味着該布局具有3 cols空間。 布置“關閉”按鈕時,所有行和列都已用完,因此“關閉”按鈕將移至默認位置(0, 0) 我只需要指定cols並保留rows -它將使用必要的行來布置子級。

其次,這不是問題,但是有一種更快的方法將所有孩子的身高設置為32dp。 您可以在GridLayout上設置row_default_heightrow_force_default ,而不是在所有子項上指定size_hintheight

最后,您的GridLayout仍然占據所有空間,這就是為什么它不在AnchorLayout居中的AnchorLayout 由於minimum_height屬性,使用GridLayout可以輕松解決此問題-將height設置為minimum_height ,將size_hint_yNone ,它將居中。

放在一起:

<LoginControl>:
    cols: 1
    row_force_default: True
    row_default_height: '32dp'
    size_hint_y: None
    height: self.minimum_height

居中的GridLayout示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM