python3 对另一个感到沮丧的 BoxLayout

python3 kivy BoxLayout over another

我查看了文档以了解如何在我的 kivy 中放置我的 Box Layout window

https://kivy.org/docs/api-kivy.uix.boxlayout.html

但我想像这样将我的 BoxLayout 放在另一个(具有透明背景)之上:

我的代码(没有我的五个透明红框)

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout

from kivy.uix.button import Button



# Boxlayout is the App class

class BoxLayoutDemo(App):

    def build(self):

        superBox        = BoxLayout(orientation='vertical')



        horizontalBox   = BoxLayout(orientation='horizontal')

        button1         = Button(text="One")

        button2         = Button(text="Two")



        horizontalBox.add_widget(button1)

        horizontalBox.add_widget(button2)



        verticalBox     = BoxLayout(orientation='vertical')

        button3         = Button(text="Three")

        button4         = Button(text="Four")



        verticalBox.add_widget(button3)

        verticalBox.add_widget(button4)



        superBox.add_widget(horizontalBox)

        superBox.add_widget(verticalBox)



        return superBox





# Instantiate and run the kivy app

if __name__ == '__main__':

    BoxLayoutDemo().run()

尝试将 boxlayouts 放在 floatlayout 中,如下所示:

from kivy.app import App
from kivy.lang import Builder


KV = """

<MyButton@Button>:
    background_color: (1,0,0,.5)

FloatLayout:

    BoxLayout:
        Button:
            text: "test"
        Button:
            text: "test"

    BoxLayout:
        orientation: "vertical"
        MyButton:
            text: "test"
        MyButton:
            text: "test"

"""


class MyApp(App):

    def build(self):
        return Builder.load_string(KV)

MyApp().run()

输出:

使用 FloatLayout as your root widget, and pos_hint: {'top': 1} so that you can place the transparent BoxLayout at the top. As for transparency, use Button's background_normal and background_color.

片段

FloatLayout:
...
    # topBox
    BoxLayout:
        Button:
            text: 'Five (with transparent red background)'
            background_normal: ''
            background_color: 0.8, 0, 0, 0.5  # 5% red
            size_hint: (0.5, 0.1)
            pos_hint: {'top': 1}

示例

main.py

from kivy.lang import Builder
from kivy.base import runTouchApp

runTouchApp(Builder.load_string('''
FloatLayout:
    size: (300, 300)

    # superBox
    BoxLayout:
        orientation: 'vertical'

        # horizontalBox
        BoxLayout:
            # orientation: 'horizontal' - default orientation is horizontal
            Button:
                text: 'One'
            Button:
                text: 'Two'

        # verticalBox
        BoxLayout:
            orientation: 'vertical'
            Button:
                text: 'Three'
            Button:
                text: 'Four'

    # topBox
    BoxLayout:
        Button:
            text: 'Five (with transparent red background)'
            background_normal: ''
            background_color: 0.8, 0, 0, 0.5  # 5% red
            size_hint: (0.5, 0.1)
            pos_hint: {'top': 1}

'''))

输出