【发布时间】:2014-06-22 08:40:23
【问题描述】:
如何在 Tkinter 中使用Label 创建超链接?
快速搜索并没有显示如何执行此操作。相反,只有在 Text 小部件中创建超链接的解决方案。
【问题讨论】:
标签: python hyperlink tkinter label
如何在 Tkinter 中使用Label 创建超链接?
快速搜索并没有显示如何执行此操作。相反,只有在 Text 小部件中创建超链接的解决方案。
【问题讨论】:
标签: python hyperlink tkinter label
将标签绑定到"<Button-1>" 事件。当它被提升时,callback 会被执行,从而在您的默认浏览器中打开一个新页面。
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()
你也可以通过改变回调来打开文件:
webbrowser.open_new(r"file://c:\test\test.csv")
【讨论】:
open_new 参数中的“r”有什么作用?
bind() 函数在pack() 它之前标记的方式越多。
"<ButtonRelease-1>"不是更好吗?
或者,如果您有多个标签并希望为所有人提供一个功能。那是假设您将链接作为文本
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.google.com", fg="blue", cursor="hand2")
lbl.pack()
lbl.bind("<Button-1>", callback)
root.mainloop()
【讨论】:
"<ButtonRelease-1>"不是更好吗?
PyPi called tkhtmlview (pip install tkhtmlview) 上有一个模块支持 tkinter 中的 HTML。它只支持一些标签,但在页面上,它说它完全支持 fro 标签(超链接的锚标签),并支持 href 属性。它需要 Python 3.4 或更高版本,支持 tcl/tk (tkinter) 和 Pillow 5.3.0 模块。我还没有尝试过这个标签,但我一般都试过这个模块,它可以工作。
举个例子:
import tkinter as tk
from tkhtmlview import HTMLLabel
root = tk.Tk()
html_label=HTMLLabel(root, html='<a href="http://www.google.com"> Google Hyperlink </a>')
html_label.pack()
root.mainloop()
【讨论】:
您可以创建一个继承自 tkinter 模块标签小部件的类,并使用其他值初始化它,当然包括链接... 然后创建它的一个实例,如下所示。
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.python.org'
link = Link(root, URL, font=('sans-serif', 20), text='Python', bg='LightBlue')
link.pack(pady=50)
root.mainloop()
【讨论】:
您可以简单地导入网络浏览器,添加一个函数并在按钮内部调用它。声明你的布局管理器。下面是代码的样子:
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.google.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()
仅供参考。它将在计算机的默认浏览器中打开
【讨论】: