python-3.x - 在 PySimpleGUI 中使用 sg.Tree() 显示目录
问题描述
我想显示PDF的目录,因为我正在使用PySimpleGUI创建 PDFViewer 。我不知道是否还有其他选项可用于创建除TreeData()
.
我的目录是一个嵌套列表,它看起来像这样:
[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]
现在,我不知道如何遍历这个列表并将所有内容放在应该的位置。例如,给定的列表应如下所示:
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
....
解决方案
在递归的帮助下,我能够实现我的目标。我正在共享该功能,以便未来的访问者立即获得解决方案。
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
其插入。第一次调用这个函数是这样的:
create_toc(toc, "", toc[0][0])
推荐阅读
- sql - 如何在 OBIEE 11g 数据模型中有效地“外连接”两个数据源(SQL 查询)?
- angular - 调用方法 createUserWithEmailAndPassword 而不进行登录
- javascript - 使用此关键字时,为什么在调试和运行时会输出不同的结果?
- c# - 如何为 VSCode 调试适配器制作单元测试用例?
- java - 如何将失败的消息从我的 Web / 应用程序服务器 (java) 重新传输到 FCM 服务器
- matlab - 40x455 大矩阵的平均值(来自 5 个会话的数据,即每个会话 40x91)
- javascript - 单击以在弹出窗口中显示特定用户的记录
- c++ - 在编译时将强类型枚举器转换为其基础类型?
- php - 获取从 PHP 传递的 JS 中的树枝变量
- c# - C# 充气城堡 FipsDRBG