关于tkinter超链接捆绑求助
当前代码one = getlist() #热榜list
two = getline() #热榜对应链接
root = Tk()
root.title('百度贴吧热榜')
sb = Scrollbar(root)
sb.pack(side=RIGHT,fill=Y)
text = Text(root,width=30,height=30,yscrollcommand=sb.set)
text.pack(side=LEFT)
sb.config(command=text.yview)
def show_arrow_cursor(event):
text.config(cursor = "arrow")
def show_xterm__cursor(event):
text.config(cursor = "xterm")
def click(event):
webbrowser.open(tow)
for i,each in enumerate(one):
text.insert(END,each)
text.tag_add("link","0.0",'1.10')
text.insert(END,'\n')
text.insert(END,'\n')
text.tag_config("link",foreground = 'blue',underline = True)
text.tag_bind("link",'<Enter>',show_arrow_cursor)
text.tag_bind("link",'<Leave>',show_xterm__cursor)
text.tag_bind("link",'<Enter>',click)
mainloop()
-------------------------------------------------------
已经获得热榜词条列表和超链接列表
现在需要打印出热榜列表,当点击热榜词条触发超链接
问题:
1、我要怎么绑定每个词条,对应超链接
2、每个词条都要添加下横线
请问下大神我要如何修改?
本帖最后由 isdkz 于 2022-5-12 11:25 编辑
参考代码:
from tkinter import *
import webbrowser
from functools import partial
one = ['测试一', '测试二', '测试三','测试四', '测试五'] #热榜list
two = [
'http://www.baidu.com',
'http://www.qq.com',
'http://www.zhihu.com',
'http://www.taobao.com',
'http://www.bing.com
] #热榜对应链接
root = Tk()
root.title('百度贴吧热榜')
sb = Scrollbar(root)
sb.pack(side=RIGHT,fill=Y)
text = Text(root,width=30,height=30,yscrollcommand=sb.set)
text.pack(side=LEFT)
sb.config(command=text.yview)
def show_arrow_cursor(event):
text.config(cursor = "arrow")
def show_xterm__cursor(event):
text.config(cursor = "xterm")
def click(event, u):
webbrowser.open(u)
for i,each in enumerate(one):
text.insert(END,each,i)
text.insert(END,'\n')
text.insert(END,'\n')
for i, j in zip(range(len(one)), two):
text.tag_config(i,foreground = 'blue',underline = True)
text.tag_bind(i,'<Enter>',show_arrow_cursor)
text.tag_bind(i,'<Leave>',show_xterm__cursor)
text.tag_bind(i,'<Button-1>',partial(click, u=j))
mainloop()
在你的代码上做了点改动,参考代码:
from tkinter import *
import webbrowser
url_dict = {
'百度': 'http://www.baidu.com',
'必应': 'https://cn.bing.com/',
'鱼C论坛': 'https://fishc.com.cn/'
}
root = Tk()
root.title('百度贴吧热榜')
sb = Scrollbar(root)
sb.pack(side=RIGHT, fill=Y)
text = Text(root, width=30, height=30, yscrollcommand=sb.set)
text.pack(side=LEFT)
sb.config(command=text.yview)
def show_arrow_cursor(event):
text.config(cursor="arrow")
def show_xterm__cursor(event):
text.config(cursor="xterm")
def click(event):
row = text.index("current").split(".")
webbrowser.open(url_line)
index = 1
url_line = {}
for i in url_dict:
text.insert(END, i)
text.tag_add(f"link{index}", f"{index}.0", f'{index}.{len(i)}')
url_line = url_dict
text.insert(END, '\n')
text.insert(END, '\n')
text.tag_config(f"link{index}", foreground='blue', underline=True)
text.tag_bind(f"link{index}", '<Enter>', show_arrow_cursor)
text.tag_bind(f"link{index}", '<Leave>', show_xterm__cursor)
text.tag_bind(f"link{index}", '<Button-1>', click)
index += 2
mainloop() isdkz 发表于 2022-5-11 20:15
参考代码:
谢谢大佬 Twilight6 发表于 2022-5-11 22:12
在你的代码上做了点改动,参考代码:
大佬这个写法更复杂,我不好理解 bhunht 发表于 2022-5-12 10:46
大佬这个写法更复杂,我不好理解
没事,我写的确实麻烦了不少
页:
[1]