【问题标题】:How to "anchor" an BoxLayout on top side inside an TabedPannel with Kivy?如何使用 Kivy 在 TabedPannel 的顶部“锚定”一个 BoxLayout?
【发布时间】:2021-02-18 03:20:42
【问题描述】:

我在 TabbedPannel 内有一个 BoxLayout,其“高度大小固定”,我希望它在顶部“锚定”,所以当我重新调整 GUI 的高度时,它会跟随顶部并在底部打开空间.

我尝试了很多方法来做到这一点,比如 pos_hint、AnchorLayout、在所有东西的底部放置一个小部件,但我无法让它工作。

这里是代码。

Example.py

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty


class TabMain(Widget):
    def __init__(self, *args):
        super(TabMain, self).__init__(*args)
        self.ids["panel"]._tab_layout.padding = '-2dp', '-2dp', '-2dp', '-2dp'

class GuiApp(App):
    def build(self):
        return TabMain()


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

gui.kv

<TabMain>
    TabbedPanel:
        id: panel
        size: root.width, root.height
        do_default_tab: False
        tab_pos: 'bottom_mid'
        tab_width: self.size[0] / 1
        canvas:
            Rectangle:
                size: self.size

        TabbedPanelItem:
            text: "Robos"
            background_color: (48/255, 48/255, 48/255, 1) if self.state == 'down' else (88/255, 88/255, 88/255, 1)
            background_down: ""
            background_normal: ""
            TabbedPanel:
                do_default_tab: False
                tab_width: 115

                TabbedPanelItem:
                    text: "Solicitar"
                    TabbedPanel:
                        tab_pos: 'bottom_right'
                        do_default_tab: False
                        TabbedPanelItem:

                            text: "Solicitar"
                            AnchorLayout:
                                anchor_y: 'top'
                                BoxLayout:

                                    orientation: "vertical"
                                    spacing: 4
                                    padding: 4
                                    pos_hint: {'y': 1}

                                    BoxLayout:
                                        size_hint: 1, None
                                        height: 30

                                        Label:
                                            text: "Login"
                                            halign: "right"
                                        TextInput:
                                            multiline: False
                                            size_hint: 1.7 , 1
                                        Label:
                                            text: "Senha"
                                        TextInput:
                                            multiline: False
                                            size_hint: 1.7, 1
                                    BoxLayout:
                                        size_hint: 1, None
                                        height: 30
                                        Label:
                                            text: "Data Inicial"
                                        TextInput:
                                            multiline: False
                                            size_hint: 1.7, 1
                                        Label:
                                            text: "Data Final"
                                        TextInput:
                                            multiline: False
                                            size_hint: 1.7, 1
                                    BoxLayout:
                                        size_hint: 1, None
                                        height: 45
                                        Label:
                                            text: "Lembrar informações dadas pelo usuario"
                                            size_hint: None, 1
                                            width: 350
                                        CheckBox:
                                            size_hint: 0.3, 1
                                        Label:
                                            size_hint: 1.4, 1
                                        Button:
                                            text: "Iniciar"
                                            size_hint: 2, 0.90
                                    BoxLayout:
                                        size_hint: 1, None
                                        height:60
                                        orientation: "vertical"
                                        spacing: 0
                                        BoxLayout:
                                            size_hint: 1, None
                                            height: 30
                                            ProgressBar:
                                                value: 25
                                            Label:
                                                size_hint: 0.1, 1
                                                text: "25.0%"
                                        BoxLayout:
                                            valign: "top"
                                            size_hint: 1, None
                                            height: 30
                                            Label:
                                                size_hint: None, None
                                                text: "Alguma informação"
                                                size: self.texture_size
                                            Label:
                                            Label:
                                                size_hint: None, None
                                                text: "Parado"
                                                size: self.texture_size
                                            Label:
                                                size_hint: 0.1, 1
                                    BoxLayout:
                                        size_hint: 1, None
                                        height: 15


                        TabbedPanelItem:
                            text: "Relatorio"
                            Label:
                                text: "Banco de Dados"

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    BoxLayout 试图使用它已分配的所有空间,因此它最终会出现在BoxLayout 顶部的空白空间。要解决这个问题,您可以使用minimum_height:

                            AnchorLayout:
                                anchor_y: 'top'
                                BoxLayout:
    
                                    orientation: "vertical"
                                    spacing: 4
                                    padding: 4
                                    pos_hint: {'y': 1}
                                    size_hint: 1, None
                                    height: self.minimum_height
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      相关资源
      最近更新 更多