首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用sg.Tree()在PySimpleGUI中显示目录

使用sg.Tree()在PySimpleGUI中显示目录
EN

Stack Overflow用户
提问于 2020-05-09 12:54:13
回答 1查看 863关注 0票数 0

我想显示一个PDF的目录,因为我正在使用PySimpleGUI创建一个PDFViewer。我不知道是否还有其他选项可以用于创建除TreeData()之外的目录。

我的目录是一个嵌套列表,它看起来如下所示:

代码语言:javascript
运行
复制
[1, 'Cover', 1]
[1, 'PART ONE OVERVIEW', 25]
[2, 'Chapter 1 Introduction', 27]
[3, '1.1 What Operating Systems Do', 28]
[3, '1.2 Computer-System Organization', 31]
[3, '1.3 Computer-System Architecture', 36]
[2, 'Chapter 2 Operating-System Structures', 79]
[3, '2.1 Operating-System Services', 79]
[3, '2.2 User and Operating-System Interface', 82]
[3, '2.3 System Calls', 86]
[1, 'PART TWO PROCESS MANAGEMENT', 127]
[2, 'Chapter 3 Processes', 129]
[3, '3.1 Process Concept', 129]

现在,我不知道如何循环遍历这个列表,并将所有的内容都放在应该的位置。例如,给定的列表应该如下所示:

代码语言:javascript
运行
复制
Cover
PART ONE OVERVIEW
    Chapter 1 Introduction
        1.1 What Operating Systems Do
        1.2 Computer-System Organization
        1.3 Computer-System Architecture
    Chapter 2 Operating-System Structures
        2.1 Operating-System Services
        ....
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-17 08:17:27

在递归的帮助下,我实现了我的目标。我正在分享这个功能,这样未来的访问者就有了解决方案。

代码语言:javascript
运行
复制
def create_toc(self, contents, parent, phead):
    cons = iter(contents)
    for content in cons:
        i = contents.index(content)
        head, text = content[0], content[1]

        if head == phead:
            i += 1
            key = ''.join(text.split())
            self.TOC_tree.insert(parent, key, text, content[2])
        else:
            j = i
            while True:
                j += 1
                if j < len(contents) and contents[j][0] != phead:
                    next(cons, None)
                else:
                    break
            self.create_toc(contents[i:j], key, head)

首先必须创建一个sg.TreeData()对象,然后向其插入Node。第一次调用此函数将如下所示:

代码语言:javascript
运行
复制
create_toc(toc, "", toc[0][0])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61697027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档