【问题标题】:Adding a hyperlink to Tkinter Treeview Values添加到 Tkinter Treeview 值的超链接
【发布时间】:2022-11-23 21:44:56
【问题描述】:

我正在使用 Tkinter 组合决策树工具。我想将超链接列中的值变成可点击的超链接。我该怎么做呢?

这是相关代码。

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 
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"))

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

#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"))

my_tree.pack(pady=20)
root.mainloop()

【问题讨论】:

    标签: python tkinter hyperlink


    【解决方案1】:

    从树视图选择中获取超链接并在浏览器中打开它应该相当简单

    import webbrowser as wb
    
    
    def open_link(event):
        tree = event.widget  # get the treeview widget
        item = tree.item(tree.focus())  # get the treeview selection
        link = item['values'][1]  # get the link from the selected row
        wb.open_new_tab(link)  # open the link in a browser tab
    
    
    # bind the selection event to 'open_link'
    my_tree.bind('<<TreeviewSelect>>', open_link)  
    

    请注意,这将在您从树视图中选择一个项目时触发,即当您单击表格的一行时,而不是专门单击第二列中的超链接。如果你想做,你要更讲究...

    import webbrowser as wb
    
    
    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)
    

    现在,只有当用户单击“超链接”列中的链接时,链接才会打开

    【讨论】:

    • 当我尝试您建议的第一个选项时,当我单击包含超链接的行时它确实有效。但是,当我单击任何其他行时,还会发生什么情况,它会打开 Windows 资源管理器到存储 python 文件的位置。如果我尝试第二种(更具体的)方法,我会得到“TypeError: string indices must be integers”
    • “不太挑剔”的第一个选项假定每个项目的第二列中始终有一个超链接。对于第二个,当你在item = tree.identify('item', event.x, event.y)之后print(item['values'])时,你看到了什么?
    • tree.identify("item",...) 返回字符串形式的项目 ID,因此 item["values"] 将引发异常。
    • @acw1668 感谢您的帮助。现在应该修好了! OP,如果您知道您的“超链接”列可能并不总是包含每个项目的内容,您可以通过在 link = tree.item(iid)['values'][1] 之后添加另一个 if 子句来进一步过滤。例如:if link: 将任何非空值视为超链接,或 if 'http' in link 查找网址……你懂的!
    猜你喜欢
    • 2023-04-06
    • 2021-04-30
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2015-11-28
    • 1970-01-01
    • 2016-01-05
    相关资源
    最近更新 更多