0

Is there way to replace the value that is displayed (in this case a very long hyperlink) in a Tkinter treeview column with something shorter (but it still open the hyperlink)? The example I have is similar to the screenshot below, except the Google link is actually a very long OneNote link. I would like to be able to replace the text that is displayed with a "Click Here". So for this example, "Click Here" would take the user to www.google.com.

enter image description here

The code I'm using to add the hyperlink is as follows:

from tkinter import *
from tkinter import ttk
import webbrowser as wb

root=Tk()
root.title('Decision Tree')
root.geometry("600x600")

my_tree = ttk.Treeview(root)

#Define the columns
my_tree['columns'] = ("Decision", "Hyperlinks", "ID")


#format the columns
my_tree.column("#0", width=250, minwidth=100)
my_tree.column("#1", width=0, stretch="No")
my_tree.column("Hyperlinks", anchor=W, width=200)
my_tree.column("ID", anchor=CENTER, width=80)


#Create Headings
my_tree.heading("#0", text="Decision", anchor=W)
my_tree.heading("#1", text="", anchor=W)
my_tree.heading("Hyperlinks", text="Hyperlinks", anchor=W)
my_tree.heading("ID", text="ID", anchor=CENTER)

#Add Data (Top Level)
my_tree.insert(parent='', index='1', iid=0, text="Problem 1", values=("", "", "1"))
my_tree.insert(parent='', index='1', iid=2, text="Problem 2", values=("", "", "3"))
my_tree.insert(parent='', index='1', iid=1, text="Problem 3", values=("", "", "2"))

my_tree.tag_configure('tag1', foreground='Blue', font=('Helvetica' ,8, 'bold', 'italic'))

#Add child level 1
my_tree.insert(parent='0', index='end', iid=6, text="Prob 1 level 2", values=("", "www.google.com", "1.1"), tags='tag1',)
my_tree.insert(parent='1', index='end', iid=7, text="Prob 3 level 2", values=("", "www.google.com", "3.1"), tags='tag1',)
my_tree.insert(parent='2', index='end', iid=8, text="Prob 2 level 2", values=("", "www.google.com", "2.1"), tags='tag1',)

#Add child level 2
my_tree.insert(parent='6', index='end', iid=9, text="Prob 1 level 3", values=("", "", "1.11"))
my_tree.insert(parent='7', index='end', iid=10, text="Prob 2 level 3", values=("", "", "2.21"))

def open_link(event):
    tree = event.widget  # get the treeview widget
    region = tree.identify_region(event.x, event.y)
    col = tree.identify_column(event.x)
    iid = tree.identify('item', event.x, event.y)
    if region == 'cell' and col == '#2':
        link = tree.item(iid)['values'][1]  # get the link from the selected row
        wb.open_new_tab(link)  # open the link in a browser tab

# bind left-click to 'open_link'
my_tree.bind('<Button-1>', open_link)

#pack to the screen
my_tree.pack(pady=20)
root.mainloop()
1
  • You can use pyshorteners module to change the long url to a shorter url. Commented Nov 25, 2022 at 14:23

1 Answer 1

2

You can store the links in a dictionary, with the key being whatever you want to display to the user. Then it’s just a matter of looking up the link when the user clicks.

Another alternative is to put the actual link in a hidden column.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually went for the dictionary method that you suggested and that seemed to work well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.