如何在Tkinter中创建带有Label的超链接? [英] How to create a hyperlink with a Label in Tkinter?
本文介绍了如何在Tkinter中创建带有Label的超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Tkinter中使用标签
创建超链接?
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.
推荐答案
将标签绑定到< Button-1>
事件。当它被引发时,回调
被执行,导致默认浏览器中的新页面打开。
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(event):
webbrowser.open_new(r"http://www.google.com")
root = Tk()
link = Label(root, text="Google Hyperlink", fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", callback)
root.mainloop()
您还可以通过将回调更改为:
You can also open files by changing the callback to:
webbrowser.open_new(r"file://c:\test\test.csv")
这篇关于如何在Tkinter中创建带有Label的超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文