首页 > 解决方案 > 试图在屏幕布局中调用布局类 - kivy

问题描述

本质上,我有多个屏幕,由屏幕管理器控制(主要用于登录),但是在主屏幕上,我将它分解为标题、导航栏和正文部分。在正文部分,我希望能够调用我有类的不同布局,并在按下导航栏上的按钮时添加和删除这些布局。

我使用 kivy 来控制 gui 的各个方面,但对它来说还很陌生。目前我有:

Builder.load_string('''
<MainMenu>:
    FloatLayout:
        canvas.before:
            Color:
                rgba: 0.235, 0.271, 0.302, 1
            Rectangle:
                pos: self.pos
                size: self.size
        BoxLayout:
            orientation: 'vertical'
            rows: 4
            Label:
                size_hint: 1,0.12
                text: "ProductName"
                text_size: self.size
                halign: 'left'
                valign: 'middle'
                font_size: 32
                color: [0.114,0.18,0.224,1]
                
            StackLayout:
                size_hint: 1,0.10
                orientation: 'lr-tb'
                #Purchase Order System
                Button:
                    id: butpos
                    text: "Purchase Order"
                    size_hint: 0.166,1
                    on_press: spaceholder.add_widget(pos)
                #Asset Assignment System
                Button:
                    id: butaas
                    text: "Asset Assignment"
                    size_hint: 0.166,1
                #Review and Revise System
                Button:
                    id: butrrs
                    text: "Review"
                    size_hint: 0.166,1
                #Administrative System
                Button:
                    id: butadm
                    text: "Administration"
                    size_hint: 0.166,1
                #Analytics and Reporting System
                Button:
                    id: butars
                    text: "Analytics"
                    size_hint: 0.166,1
                #Placeholder, possibly documentation
                Button:
                    id: butbut
                    text: "Placeholder"
                    size_hint: 0.166,1
                    
            Label:
                canvas.before:
                    Color:
                        rgba: 0.259, 0.643, 0.937, 1
                    Rectangle:
                        pos: self.pos
                        size: self.size
                size_hint: 1,0.005
                
            Widget:
                id: spaceholder

<PurchaseOrder>
    id: pos
    orientation: 'lr-tb'
    #Asset Details
    Label:
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: "Asset Requirements"
            Label:
                text: "Asset1: "
            TextInput:
                id: as1count
            Label:
                text: "Asset2: "
            TextInput:
                id: as2count
                
''')
class MainMenu(Screen):
    pass
class PurchaseOrder(StackLayout):
    pass

class MainApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(MainMenu(name='mainmenu'))
        return sm

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

PurchaseOrder 是一个我喜欢添加或删除的类,就像我打算为其他导航选项设置的类一样。我尝试了很多方法,但我认为我错过了一些东西。我要么被“pos”抛出未定义,要么当我尝试不同的组合时“spaceholder”具有错误的属性。

标签: pythonkivy

解决方案


您可以通过修改Purchase Order Button

on_press: spaceholder.add_widget(Factory.PurchaseOrder())

这需要添加到您kv的导入Factory

#:import Factory kivy.factory.Factory

我建议您使用 aLayoutspaceholder利用它们的尺寸和定位功能。例如,使用AnchorLayout自动居中其子级(但仅限于单个子级)。像这样:

        AnchorLayout:
            id: spaceholder

推荐阅读