Kivy - 组合两种不同的浮动布局并对齐浮动按钮

Kivy - Combining two different float layouts and aligning the float buttons

我正在尝试构建一个具有树视图的应用程序。我能够组合两个浮动布局,但无法正确对齐它们。如果有人可以查看代码并让我知道我哪里做错了,我将不胜感激。 在下面的代码中,您会发现 'Student Search'、树视图位于顶部。我想把它放在 'Class 1' 按钮下面。 任何帮助将不胜感激!

Main.py代码

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.core.image import Image as CoreImage
from kivy.uix.treeview import TreeViewLabel, TreeView



def populate_tree_view(tree_view, parent, node):
    if parent is None:
        tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
                                                     is_open=False))
    else:
        tree_node = tree_view.add_node(TreeViewLabel(text=node['node_id'],
                                                     is_open=False), parent)

    for child_node in node['children']:
        populate_tree_view(tree_view, tree_node, child_node)

tree = {'node_id': 'Student Search','children':[{'node_id':'Number 1 ABC','children':[]},{'node_id':'Number 2 XYZ','children':[]},{'node_id':'Number 4 IJK','children':[]}]}
class TreeWidget(FloatLayout):
    def __init__(self, **kwargs):
        super(TreeWidget, self).__init__(**kwargs)

        tv = TreeView(root_options=dict(text='Users'),
                      hide_root=True,
                      indent_level=10)

        populate_tree_view(tv, None, tree)

        self.add_widget(tv)
        

class MainLayout(FloatLayout):
    
    pass
    

class HomePageApp(App):
    def build(self):
        return MainLayout()
   

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

这是kv文件

################# this button will be used for float layout #############################
#<LogoButton@Button>: 
#   size_hint: .1, 0.1
#   background_normal: 'logo.jpg'
#   background_down: 'logo.jpg'
<Button@Button>: 
    font_size: 15
    color: 1,1,1,1
    
    size_hint: 0.32, 0.07
<Page@Button>: 
    font_size: 25
    background_color: 0,0,0,0
    color: 1,1,1,1
    size_hint: .96, 0.1
<TreeWidget>:
    font_size: 40

##########################################################################################
<MainLayout>:
    treetest: treeview
    Button:
        text:"Class 1"
        
        pos_hint: {'x': 0.025, 'y':.8}
    Button:
        text:"Class 2"
        pos_hint: {'x': 0.345, 'y':.8}
    Button:
        text:"Class 3"
        pos_hint: {'x': 0.665, 'y':.8}  
    Page:
        text:"Home Page"
        pos_hint: {'x': 0.025, 'y':.9}
    #LogoButton:
    #   pos_hint: {'x': 0.025, 'y':.9}

    
    
    TreeWidget:
        id:treeview
        pos_hint: {'x': 0, 'y':0}
        ```

您可以在声明 TreeView 时传递 pos_hint 以指定您想要的位置。

所以试试这个:

tv = TreeView(root_options=dict(text='Users'),
              hide_root=True,
              indent_level=10,
              pos_hint={'x': 0.025, 'top': 0.75})