【问题标题】:Opening PDFs using Treeview使用 Treeview 打开 PDF
【发布时间】:2021-10-20 17:10:40
【问题描述】:

我的桌面上有一个文件,其中有 100 多个子文件夹,它们都分支出来以组织大量 PDF。我正在尝试制作一个使用 GUI 来更好地访问和打开这些 PDF 的应用程序。 Treeview 将是完美的,但我无法弄清楚如何使用除按钮之外的任何东西来实际打开文件。有人可以告诉我如何使用这些来查找文件路径并打开 pdf 吗?谢谢。

编辑:

基本上,我有一棵看起来像这样的树:

tree = ttk.Treeview(vender_class)
tree.pack(fill=BOTH)
tree.insert(parent='', index='end', iid=1, text="Thresholds")
tree.insert(parent='1', index='end', iid=2, text="Butt Hung")
tree.insert(parent='2', index='end', iid=3, text="THBH")
tree.insert(parent='1', index='end', iid=4, text="Center Hung")
tree.insert(parent='4', index='end', iid=5, text="THCH")
tree.insert(parent='1', index='end', iid=6, text="Offset Hung")
tree.insert(parent='6', index='end', iid=7, text="THOH")
tree.insert(parent='1', index='end', iid=8, text="Other")
tree.insert(parent='8', index='end', iid=9, text="THO")

open_button=ttk.Button(vender_class, text="Open PDF", command=openfile)
open_button.pack()

我希望能够简单地双击最终项目(即“THBH”)并在 Bluebeam 上打开 PDF。如果这不起作用,那么如何将其链接到按钮,以便如果选择“THBH”并按下按钮,它会以这种方式打开 PDF?

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: class pdf tkinter treeview filepath


【解决方案1】:

首先为双击打开PDF的行附加一个特定的标签。

然后在Treeview小部件上绑定双击事件,然后在回调中检查双击的项目是否有特定的标签。如果有,根据点击项目的text打开PDF文件。

tree.insert(parent='', index='end', iid=1, text="Thresholds")
tree.insert(parent='1', index='end', iid=2, text="Butt Hung")
tree.insert(parent='2', index='end', iid=3, text="THBH", tags='pdf')
tree.insert(parent='1', index='end', iid=4, text="Center Hung")
tree.insert(parent='4', index='end', iid=5, text="THCH", tags='pdf')
tree.insert(parent='1', index='end', iid=6, text="Offset Hung")
tree.insert(parent='6', index='end', iid=7, text="THOH", tags='pdf')
tree.insert(parent='1', index='end', iid=8, text="Other")
tree.insert(parent='8', index='end', iid=9, text="THO", tags='pdf')

def on_double_click(event):
    iid = tree.focus() # get the iid of the selected item
    tags = tree.item(iid, 'tags') # get tags attached
    if 'pdf' in tags:
        text = tree.item(iid, 'text') # get the text of selected item
        print('open PDF for', text)
        # open PDF based on text
        ...

tree.bind('<Double-Button-1>', on_double_click)

【讨论】:

  • 感谢您的出色工作!我对其进行了一些修改,因此我不需要添加标签(我给出的示例树是 50 多个树中最小的一个),但它完全按照我的需要工作。再次感谢!
最近更新 更多