Python 3.x 在Tkinter的文本小部件中添加指向文本的链接
我正在使用Tkinter和bs4用python创建一个歌曲通知程序。 我从一个网站上提取了歌曲及其相应的URL。 我使用文本小部件来存储歌曲,并将它们的URL作为字典中的键值 现在我想添加歌曲名称的链接(存储在文本小部件中),这样当我单击某首歌曲时,它的url就会在chrome中打开 以下是代码片段:Python 3.x 在Tkinter的文本小部件中添加指向文本的链接,python-3.x,tkinter,beautifulsoup,Python 3.x,Tkinter,Beautifulsoup,我正在使用Tkinter和bs4用python创建一个歌曲通知程序。 我从一个网站上提取了歌曲及其相应的URL。 我使用文本小部件来存储歌曲,并将它们的URL作为字典中的键值 现在我想添加歌曲名称的链接(存储在文本小部件中),这样当我单击某首歌曲时,它的url就会在chrome中打开 以下是代码片段: from tkinter import * import webbrowser from bollywood_top_50 import bollywood_songs_list , bollyw
from tkinter import *
import webbrowser
from bollywood_top_50 import bollywood_songs_list , bollywood_songs_dict
from international_top_50 import international_songs_list
b_songs_list = bollywood_songs_list()
b_songs_dict = bollywood_songs_dict()
i_songs_list = international_songs_list()
root = Tk()
S = Scrollbar(root)
T = Text(root, height=20, width=30,cursor="hand2")
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
def callback_a():
T.delete(1.0,END)
for songs in b_songs_list:
T.insert(END, songs + '\n')
def callback_b():
T.delete(1.0,END)
for songs in i_songs_list:
T.insert(END, songs + '\n')
bollywood_button = Button(root,text="Bollywood-Top-50", command=callback_a)
bollywood_button.pack()
international_button = Button(root,text="International-Top-50", command=callback_b)
international_button.pack()
以下是示例输出:
我没有问题。创建一个名为“tkHyperlinkManager.py”的文件,该文件包含:
from tkinter import *
class HyperlinkManager:
def __init__(self, text):
self.text = text
self.text.tag_config("hyper", foreground="blue", underline=1)
self.text.tag_bind("hyper", "<Enter>", self._enter)
self.text.tag_bind("hyper", "<Leave>", self._leave)
self.text.tag_bind("hyper", "<Button-1>", self._click)
self.reset()
def reset(self):
self.links = {}
def add(self, action):
# add an action to the manager. returns tags to use in
# associated text widget
tag = "hyper-%d" % len(self.links)
self.links[tag] = action
return "hyper", tag
def _enter(self, event):
self.text.config(cursor="hand2")
def _leave(self, event):
self.text.config(cursor="")
def _click(self, event):
for tag in self.text.tag_names(CURRENT):
if tag[:6] == "hyper-":
self.links[tag]()
return
我没有问题。创建一个名为“tkHyperlinkManager.py”的文件,该文件包含:
from tkinter import *
class HyperlinkManager:
def __init__(self, text):
self.text = text
self.text.tag_config("hyper", foreground="blue", underline=1)
self.text.tag_bind("hyper", "<Enter>", self._enter)
self.text.tag_bind("hyper", "<Leave>", self._leave)
self.text.tag_bind("hyper", "<Button-1>", self._click)
self.reset()
def reset(self):
self.links = {}
def add(self, action):
# add an action to the manager. returns tags to use in
# associated text widget
tag = "hyper-%d" % len(self.links)
self.links[tag] = action
return "hyper", tag
def _enter(self, event):
self.text.config(cursor="hand2")
def _leave(self, event):
self.text.config(cursor="")
def _click(self, event):
for tag in self.text.tag_names(CURRENT):
if tag[:6] == "hyper-":
self.links[tag]()
return
怎么样。当我尝试导入tkHyperlinkManager时…它不会导入任何东西..但当我使用tkHyperlinkManager运行程序时。。没有出现这样的模块错误..怎么样.当我尝试导入tkHyperlinkManager时…它不会导入任何内容..但当我使用tkHyperlinkManager运行程序时。。不存在此类模块错误。。