【问题标题】:Kivy - Combining two different float layouts and aligning the float buttonsKivy - 结合两种不同的浮动布局并对齐浮动按钮
【发布时间】:2020-12-25 18:58:32
【问题描述】:

我正在尝试构建一个带有树视图的应用程序。我能够组合两个浮动布局,但无法正确对齐它们。如果有人可以查看代码并让我知道我做错了什么,我将不胜感激。 在下面的代码中,您会发现“学生搜索”树视图位于顶部。我试图将其放在“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}
        ```

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

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

    所以试试这个吧:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多