在 Tkinter 的文本小部件中添加文本链接

Abh*_*rma 2 tkinter beautifulsoup python-3.x

我正在使用 Tkinter 和 bs4 在 python 中创建一个歌曲通知程序。我已经从网站中提取了歌曲及其相应的网址。我使用文本小部件来存储歌曲并将其网址作为字典中的键值。

现在我想添加歌曲名称的链接(存储在文本小部件中),以便当我单击特定歌曲时,它的网址会在 chrome 中打开。

这是代码片段:

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()
Run Code Online (Sandbox Code Playgroud)

这是示例输出:

在此输入图像描述

fig*_*eam 6

我没有问题。创建一个名为“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
Run Code Online (Sandbox Code Playgroud)

将其导入您的程序并插入链接。我提供了看起来合理的样本数据。宝莱坞歌曲列表带有超链接,而国际歌曲列表则没有。我只是对该机制的演示。您必须自己编写所有回调和 Web 界面。

from tkinter import *
from tkHyperlinkManager import *

b_songs_list  = ['Bollywood song 1','Bollywood song 2','Bollywood song 3']
i_songs_list = ['International song 1','International song 2','International song 3']

root = Tk()
S = Scrollbar(root)
T = Text(root, height=20, width=30,cursor="hand2")
hyperlink = HyperlinkManager(T)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)    

def click1():
    print('click1')

def callback_a():   # Bollywood songs WITH hyperlinks
    T.delete(1.0,END)
    for songs in b_songs_list:
       T.insert(END, songs, hyperlink.add(click1))
       T.insert(END, '\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()
Run Code Online (Sandbox Code Playgroud)