首页 > 解决方案 > 如何使用 KivyMD 的 BoxLayout

问题描述

为什么我只能使用 BoxLayout 的垂直方向来水平移动按钮,而只能使用水平方向来垂直移动按钮?例如:

from kivy.lang import Builder
from kivymd.app import MDApp
    KV = '''
    Screen:
        BoxLayout:
            orientation: 'vertical'
            MDRaisedButton:
                pos_hint: {'center_x': .3,'center_y': .5}
    '''
    class Test(MDApp):
        def build(self):
            return Builder.load_string(KV)
    Test().run()

水平方向,也是默认方向:

from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
Screen:
    BoxLayout:
        orientation: 'horizontal'
        MDRaisedButton:
            pos_hint: {'center_x': .3,'center_y': .5}
'''
class FindWord(MDApp):
    def build(self):
        return Builder.load_string(KV)
FindWord().run()

如何在两个轴上自由移动小部件?请帮忙

标签: pythonpython-3.xkivykivy-languagekivymd

解决方案


from kivy.lang import Builder

from kivymd.app import MDApp

KV = '''
MDScreen:

    MDBoxLayout:
        adaptive_size: True
        pos_hint: {'center_x': .3, 'center_y': .5}

        MDRaisedButton:
            text: "MDRaisedButton"
'''


class FindWord(MDApp):
    def build(self):
        return Builder.load_string(KV)


FindWord().run()

https://kivy.org/doc/stable/api-kivy.uix.floatlayout.html

https://kivy.org/doc/stable/api-kivy.uix.boxlayout.html


推荐阅读