簡體   English   中英

Python Kivy屏幕管理器

[英]Python Kivy Screen Manager

<LowerBoxNav>:
    BoxLayout:
        orientation: 'horizontal'
        LoginButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                root.manager.current='LoginScreen#'

        NextButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                root.manager.current='ScreenThree#'

<HomePage>:
    LayoutsApp:
        LabelsApp:
            pos_hint: {'center_x': .5, 'center_y': .7}
            text: root.homepageusernametext
        NextButtonsApp:
            pos_hint: {'center_x': .5, 'center_y': .5}
            on_press: 
                root.manager.current='ScreenThree#'
    LowerBoxNav:

我在.kv文件中有兩個小部件:-主頁-它繼承自Screen,並具有screen.manager屬性,該屬性用於管理屏幕過渡-LowerBoxNav-這是一種盒子布局。 本質上,我希望每個頁面都具有這種布局。 在框布局內有2個按鈕-LoginButtonsApp和NextButtonsApp。

我的問題如下:-使用上面的代碼,我得到錯誤AttributeError:'LowerBoxNav'對象沒有屬性'manager'-我還嘗試為HomePage屏幕內LowerBoxNav小部件中的每個按鈕添加on_press-在這種情況下,我每個按鈕有2個

希望在我的應用程序的每個屏幕上都具有相同的LowerBoxNav的任何幫助。

root表示<>中的規則或窗口小部件,對您而言意味着BoxLayout ,因此沒有可用的manager ,並且您得到AttributeError

您必須通過Screen小部件訪問manager屬性,或者使自己成為規則,其中Screen位於頂部,而BoxLayout位於其中。 而且, Screen必須是ScreenManager的子級,否則它將無法工作。

編輯:更改root.manager.currentroot.parent.manager.current

<LowerBoxNav>:
    BoxLayout:
        orientation: 'horizontal'
        LoginButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                app.root.ids.sm.current='LoginScreen#'

        NextButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                app.root.ids.sm.current='ScreenThree#'

<HomePage>:
    id: sm
    LayoutsApp:
        LabelsApp:
            pos_hint: {'center_x': .5, 'center_y': .7}
            text: root.homepageusernametext
    LowerBoxNav:
    ScreenThree:

給ScreenManager設置一個sm的ID,然后將按鈕設置為on_press:app.root.ids.sm.current =“ Whatever”。 然后確保使用名稱定義頁面,並將該名稱傳遞給屏幕管理器。 因此,在您的kv文件中,例如screenthree

<ScreenThree>:
    name: ' ScreenThree' #this is what you would call for the on_press event
    Label:
        text: 'Hello'

暫無
暫無

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

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