Kivy v1.11.1嵌套框布局

2024-09-29 23:22:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Kivy创建了一个嵌套的盒子布局,这很有效。但是我需要在Python代码中调用三个类。有没有更优雅的方法?例如,在Python中只有一个类

Python代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class HorizLayout1(BoxLayout):
    pass
class HorizLayout2(BoxLayout):
    pass

class VertLayout(BoxLayout):
    pass


class KivyTestsApp(App):
    def build(self):
        return VertLayout()

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

千伏安:

<HorizLayout1>:
    orientation: "horizontal"
    Button:
        text: "1"
    Button:
        text: "2"

<HorizLayout2>:
    orientation: "horizontal"
    Button:
        text: "3"
    Button:
        text: "4"

<VertLayout>:
    orientation: "vertical"
    HorizLayout1:
    HorizLayout2:

Tags: 代码textfromimportappbuttonpassclass
1条回答
网友
1楼 · 发布于 2024-09-29 23:22:13

您的py可以是:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

class VertLayout(BoxLayout):
    pass

class KivyTestsApp(App):
    def build(self):
        return VertLayout()

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

。。。你的kv可能是这样的:

<VertLayout>:
    orientation: "vertical"
    BoxLayout:
        Button:
            text: "1"
        Button:
            text: "2"
    BoxLayout:
        Button:
            text: "3"
        Button:
            text: "4"

相关问题 更多 >

    热门问题