python3 kivy BoxLayout超过另一个

时间:2018-05-25 08:42:49

标签: python kivy boxlayout

我看了一下文档,知道如何将我的Box Layout放在我的kivy窗口中

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

但我希望将BoxLayout放在另一个(透明背景)上,如下所示:

enter image description here

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

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()

2 个答案:

答案 0 :(得分:1)

尝试将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()

输出:

enter image description here

答案 1 :(得分:1)

使用FloatLayout作为根窗口小部件,并使用pos_hint: {'top': 1},以便将透明BoxLayout置于顶部。至于透明度,请使用Button 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}

'''))

输出

Img01