我的桌面上有一個檔案,它有 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?
uj5u.com熱心網友回復:
首先為將在雙擊時打開 PDF 的行附加一個特定標簽。
然后在Treeviewwidget上系結雙擊事件,然后在回呼中檢查雙擊的item是否有特定的標簽。如果有,請根據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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/332992.html
