如何在 Tkinter 中创建带有标签的超链接? [英] How to create a hyperlink with a Label in Tkinter?
本文介绍了如何在 Tkinter 中创建带有标签的超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Tkinter 中使用 Label
创建超链接?
How do you create a hyperlink using a Label
in Tkinter?
快速搜索并未显示如何执行此操作.相反,只有在 Text
小部件中创建超链接的解决方案.
A quick search did not reveal how to do this. Instead there were only solutions to create a hyperlink in a Text
widget.
推荐答案
将标签绑定到 "
事件.当它被引发时, callback
被执行,导致在你的默认浏览器中打开一个新页面.
Bind the label to "<Button-1>"
event. When it is raised the callback
is executed resulting in a new page opening in your default browser.
from tkinter import *
import webbrowser
def callback(url):
webbrowser.open_new(url)
root = Tk()
link1 = Label(root, text="Google Hyperlink", fg="blue", cursor="hand2")
link1.pack()
link1.bind("<Button-1>", lambda e: callback("http://www.google.com"))
link2 = Label(root, text="Ecosia Hyperlink", fg="blue", cursor="hand2")
link2.pack()
link2.bind("<Button-1>", lambda e: callback("http://www.ecosia.org"))
root.mainloop()
您还可以通过将回调更改为:
You can also open files by changing the callback to:
webbrowser.open_new(r"file://c: est est.csv")
这篇关于如何在 Tkinter 中创建带有标签的超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文