如何在Tkinter中使用标签创建超链接?
你怎么用 标签 在 Tkinter 中创建一个 超链接
呢?
我快速搜索了一下,发现没有找到相关的做法。结果只找到了一些关于如何在 文本
组件中创建超链接的解决方案。
8 个回答
0
你可以创建一个类,这个类是从tkinter模块的标签控件(label widget)继承而来的。在这个类里,你可以添加一些额外的值,包括链接等等……然后你可以像下面这样创建这个类的一个实例。
import tkinter as t
import webbrowser
class Link(t.Label):
def __init__(self, master=None, link=None, fg='grey', font=('Arial', 10), *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.master = master
self.default_color = fg # keeping track of the default color
self.color = 'blue' # the color of the link after hovering over it
self.default_font = font # keeping track of the default font
self.link = link
""" setting the fonts as assigned by the user or by the init function """
self['fg'] = fg
self['font'] = font
""" Assigning the events to private functions of the class """
self.bind('<Enter>', self._mouse_on) # hovering over
self.bind('<Leave>', self._mouse_out) # away from the link
self.bind('<Button-1>', self._callback) # clicking the link
def _mouse_on(self, *args):
"""
if mouse on the link then we must give it the blue color and an
underline font to look like a normal link
"""
self['fg'] = self.color
self['font'] = self.default_font + ('underline', )
def _mouse_out(self, *args):
"""
if mouse goes away from our link we must reassign
the default color and font we kept track of
"""
self['fg'] = self.default_color
self['font'] = self.default_font
def _callback(self, *args):
webbrowser.open_new(self.link)
root = t.Tk()
root.title('Tkinter Links !')
root.geometry('300x200')
root.configure(background='LightBlue')
URL = 'www.example.org'
link = Link(root, URL, font=('sans-serif', 20), text='Python', bg='LightBlue')
link.pack(pady=50)
root.mainloop()
3
你可以简单地导入一个叫做webbrowser的模块,然后添加一个函数,在按钮里面调用这个函数。接下来,声明你的布局管理器。下面是代码的样子:
from tkinter import *
import webbrowser
# Displaying the root window
window = Tk()
window.config(padx=100, pady=100)
# Function Declaration
def callback():
webbrowser.open_new("https://www.example.com/")
# Button Declaration
your_variable_name = Button(text="button_name", command=callback)
your_variable_name.grid(column=2, row=3)
# keeping the Tkinter Interface running
window.mainloop()
顺便说一下,它会在你电脑默认的浏览器中打开。
4
在 PyPi上,有一个叫做 tkhtmlview 的模块(可以用 pip install tkhtmlview
安装),它可以在 tkinter 中支持 HTML。这个模块只支持一些 HTML 标签,但页面上说它对 <a>
标签(用于超链接)有完全支持,并且支持 href 属性。使用这个模块需要 Python 3.4 或更高版本,并且需要支持 tcl/tk(也就是 tkinter)和 Pillow 5.3.0 模块。我还没有尝试过 <a>
标签,但我试过这个模块的其他功能,效果不错。
举个例子:
import tkinter as tk
from tkhtmlview import HTMLLabel
root = tk.Tk()
html_label=HTMLLabel(root, html='<a href="http://www.example.com"> Hyperlink </a>')
html_label.pack()
root.mainloop()
11
另外,如果你有多个标签,并且想用一个函数来处理所有的标签。这里假设你把链接当作文本来使用。
import tkinter as tk
import webbrowser
def callback(event):
webbrowser.open_new(event.widget.cget("text"))
root = tk.Tk()
lbl = tk.Label(root, text=r"http://www.example.com", fg="blue", cursor="hand2")
lbl.pack()
lbl.bind("<Button-1>", callback)
root.mainloop()
56
将标签绑定到 "<Button-1>"
事件上。当这个事件被触发时,callback
就会执行,这样会在你的默认浏览器中打开一个新页面。
from tkinter import *
import webbrowser
def callback(url):
webbrowser.open_new(url)
root = Tk()
link1 = Label(root, text="Hyperlink", fg="blue", cursor="hand2")
link1.pack()
link1.bind("<Button-1>", lambda e: callback("http://www.example.com"))
link2 = Label(root, text="Hyperlink", fg="blue", cursor="hand2")
link2.pack()
link2.bind("<Button-1>", lambda e: callback("http://www.example.org"))
root.mainloop()
你也可以通过修改回调函数来打开文件,具体可以参考下面的内容:
webbrowser.open_new(r"file://c:\test\test.csv")