Kivy侧边栏+内容布局

2024-10-05 14:27:45 发布

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

我想在我的kivy应用程序上有一个固定宽度的右侧边栏,上面有一个按钮列表和一个主要的绘图区域,我不确定哪种方法是正确的(也就是说,哪种布局),到目前为止我的位置是:

布局pp.py。。。你知道吗

from kivy.app import App

class layoutApp(App):
    pass

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

布局PP.kv。。。你知道吗

BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        Button:
            size_hint_x: 2
        BoxLayout:
            orientation: 'vertical'
            size_hint_x: 0.5
            Button:
            Button:
            Button:
            Button:

产生:

enter image description here

问题是大小是相对的,即右侧边栏的宽度根据屏幕抓取/调整大小而变化,这不是预期的行为。你知道吗

谢谢!你知道吗


Tags: app应用程序列表size宽度button布局按钮
2条回答

对@Johnèu Anderson稍作修改,上面的按钮很窄:

BoxLayout:
    id: top_box
    orientation: 'vertical'
    BoxLayout:
        Button:
            size_hint_x: None
            width: top_box.width - bottom_box.width
        BoxLayout:
            padding: 4
            id: bottom_box
            orientation: 'vertical'
            size_hint_x: None
            width: 200
            spacing: 2
            Button:
                id: button_1
                background_normal: ''
                background_color: .2, .2, .2, 1
                text: 'Button 1'
                color: .6, .6, .6, 1
                size_hint_x: None
                size_hint_y: None
                width: 192
                height: 40
            Button:
                id: button_2
                background_normal: ''
                background_color: .2, .2, .2, 1
                text: 'Button 2'
                color: .6, .6, .6, 1
                size_hint_x: None
                size_hint_y: None
                width: 192
                height: 40
            Button:
                id: button_3
                background_normal: ''
                background_color: .2, .2, .2, 1
                text: 'Button 3'
                color: .6, .6, .6, 1
                size_hint_x: None
                size_hint_y: None
                width: 192
                height: 40
            Button:
                id: button_4
                background_normal: ''
                background_color: .2, .2, .2, 1
                text: 'Button 4'
                color: .6, .6, .6, 1
                size_hint_x: None
                size_hint_y: None
                width: 192
                height: 40
            Widget:


结果:

enter image description here

您可以设置侧边栏的宽度,然后使用ids使大按钮的宽度取决于此:

BoxLayout:
    id: top_box
    orientation: 'vertical'
    BoxLayout:
        Button:
            size_hint_x: None
            width: top_box.width - bottom_box.width
        BoxLayout:
            id: bottom_box
            orientation: 'vertical'
            size_hint_x: None
            width: 150
            Button:
            Button:
            Button:
            Button:

相关问题 更多 >