I have a file on my desktop that has over 100 subfolders all branching out to organize a ton of PDFs. I'm trying to make an app that uses a GUI to better access and open these PDFs. Treeview would be perfect, but I can't figure out how to actually open files using anything but a button. Can someone please tell me how to use these to find a filepath and open a pdf? Thanks.
EDIT:
basically, I have a tree that looks like this:
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()
I would prefer being able to simply double click the end item (i.e. "THBH") and it open the PDF on Bluebeam. If that can't work then how do you link it to the button so that if "THBH" is selected and the button is pushed, it opens the PDF that way?
CodePudding user response:
First attach a specific tag for the rows which will open PDF when double-clicked.
Then bind double-click event on the Treeview
widget and then in the callback check whether the double-clicked item has the specific tag. If it has, open the PDF file based on the text
of the clicked item.
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)