python3 kivy BoxLayout在anoth上

2024-09-28 04:24:52 发布

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

我看了文档,想知道如何在我的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()

Tags: textfromimportaddappbuttonwidget盒子
2条回答

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

使用FloatLayout作为根小部件,pos_hint: {'top': 1}这样就可以将透明的BoxLayout放在顶部。至于透明度,使用按钮的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}

示例

你知道吗主.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

相关问题 更多 >

    热门问题