首页 > 解决方案 > Kivy Treeview 位置问题

问题描述

我正在尝试创建一个包含 2 个部分的布局,每个部分都有一个标签,下面有一个视图(TreeView左侧和右侧显示详细信息的窗口,例如“单击此节点并查看详细信息”)。我已经确定了标签的大小和位置,但是当我添加 TreeView 时,它不会在标签下放置,而是将两个标签都推到上面,无论位置如何。

这是当前的结果: 在此处输入图像描述

我的main.py档案——

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.treeview import TreeView, TreeViewLabel


class MainScreen(BoxLayout):

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

        bridge_view = TreeView(root_options=dict(text='Clients'),
                               hide_root=False, indent_level=4,
                               pos_hint={"x": 0, "y": 0},
                               size_hint=(0.1, 0.5))

        clients = {'client1': 'Connection A',
                   'client2': 'Connection B'}

        for client in clients.keys():
            node = bridge_view.add_node(TreeViewLabel(text=client))
            bridge_name = clients[client]
            bridge = bridge_view.add_node(TreeViewLabel(text=bridge_name), node)

        self.add_widget(bridge_view)



class ConnectionApp(App):

    def build(self):
        self.title = 'My Support App'
        return MainScreen()

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

这是我的 kv 文件——

<Label>:
    font_size: 30


<MainScreen>:

    Label:
        text: "Connections"
        size_hint: 0.1, 0.5
        pos_hint: {"left": 0.2, "top": 1.2}
        color: 1,0,1,1
        outline_color: 1,0,1,1


    Label:
        text: "Modules"
        size_hint: 0.1, 0.5
        pos_hint: {"right": 0.2, "top": 1.2}

对我来说似乎很奇怪的另一件事是我必须如何使用pos_hint才能使标签间距起作用。据我了解,这些值应该在 0-1 之间。我读过的一个教程表明了很多——

Pos_hint 给出位置的提示,该位置在 0 和 1 之间相对测量,其中 1 是“完全”某物,0 是“不是”某物。

有谁知道为什么要为“top”使用大于 1 的值才能将这些标签放在顶部?我猜这可能暗示了为什么我的布局没有正确显示。

标签: pythontreeviewkivy

解决方案


标签下的树视图

解决方案是使用 GridLayout 作为 BoxLayout 的子级(根小部件,MainScreen),Labels 和 TreeViews 作为 GridLayout 的子级。添加了一个 ObjectProperty,container将其连接到在 kv 文件中创建的 GridLayout 小部件(TreeView 小部件的占位符)。详情请看示例。

示例 - 标签下的 TreeView

主文件

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.treeview import TreeView, TreeViewLabel
from kivy.properties import ObjectProperty
from kivy.lang import Builder

Builder.load_string('''
#:kivy 1.10.0

<Label>:
    font_size: 30


<MainScreen>:
    container: tree_view
    orientation: 'vertical'

    GridLayout:
        cols: 2
        size_hint: 0.5, 0.1

        Label:
            canvas.before:
                Color:
                    rgba: 1, 0, 0, 1    # red
                Rectangle:
                    size: self.size
                    pos: self.pos
            text: "Connections"
            color: 1,0,1,1
            outline_color: 1,0,1,1


        Label:
            canvas.before:
                Color:
                    rgba: 0, 0, 1, 1    # blue
                Rectangle:
                    size: self.size
                    pos: self.pos
            text: "Modules"

    GridLayout:
        cols: 1
        id: tree_view
''')


class MainScreen(BoxLayout):
    container = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

        bridge_view = TreeView(root_options=dict(text='Clients'),
                               hide_root=False, indent_level=4,
                               pos_hint={"x": 0, "y": 0},
                               size_hint=(0.1, 0.5))

        clients = {'client1': 'Connection A',
                   'client2': 'Connection B'}

        for client in clients.keys():
            node = bridge_view.add_node(TreeViewLabel(text=client))
            bridge_name = clients[client]
            bridge = bridge_view.add_node(TreeViewLabel(text=bridge_name), node)

        self.container.add_widget(bridge_view)


class DemoApp(App):
    def build(self):
        return MainScreen()


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

输出 - 标签下的 TreeView

Img01 - 标签下的 TreeView

pos_hint: {'top': 1}

您必须使用大于 1 的值的top原因是 Label 小部件的高度为 0.5 ( size_hint: 0.1, 0.5)。使用的解决方案pos_hint: {'top': 1}是减少size_hint_y,如片段所示。

在我的示例中,为可视化/演示添加了 Label 的画布颜色。

片段

Label:
    ...
    size_hint: 0.1, 0.1
    pos_hint: {"top": 1}

示例 - pos_hint

.kv 文件

#:kivy 1.10.0

<Label>:
    font_size: 30


<MainScreen>:

    Label:
        canvas.before:
            Color:
                rgba: 1, 0, 0, 1    # red
            Rectangle:
                size: self.size
                pos: self.pos
        text: "Connections"
        size_hint: 0.1, 0.1
        pos_hint: {"left": 0.2, "top": 1}
        color: 1,0,1,1
        outline_color: 1,0,1,1


    Label:
        canvas.before:
            Color:
                rgba: 0, 0, 1, 1    # blue
            Rectangle:
                size: self.size
                pos: self.pos
        text: "Modules"
        size_hint: 0.1, 0.1
        pos_hint: {"right": 0.2, "top": 1}

输出 - pos_hint

Img01 - pos_hint


推荐阅读