鱼C论坛

 找回密码
 立即注册
查看: 1450|回复: 5

[已解决]关于tkinter超链接捆绑求助

[复制链接]
发表于 2022-5-11 17:51:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
当前代码

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、每个词条都要添加下横线


请问下大神我要如何修改?
最佳答案
2022-5-11 20:15:03
本帖最后由 isdkz 于 2022-5-12 11:25 编辑

参考代码:
  1. from tkinter import *
  2. import webbrowser
  3. from functools import partial
  4. one = ['测试一', '测试二', '测试三','测试四', '测试五'] #热榜list

  5. two = [
  6.     'http://www.baidu.com',
  7.     'http://www.qq.com',
  8.     'http://www.zhihu.com',
  9.     'http://www.taobao.com',
  10.     'http://www.bing.com
  11.     ] #热榜对应链接


  12. root = Tk()
  13. root.title('百度贴吧热榜')

  14. sb = Scrollbar(root)

  15. sb.pack(side=RIGHT,fill=Y)

  16. text = Text(root,width=30,height=30,yscrollcommand=sb.set)

  17. text.pack(side=LEFT)

  18. sb.config(command=text.yview)


  19. def show_arrow_cursor(event):
  20.     text.config(cursor = "arrow")

  21. def show_xterm__cursor(event):
  22.     text.config(cursor = "xterm")

  23. def click(event, u):
  24.     webbrowser.open(u)
  25.    
  26. for i,each in enumerate(one):
  27.     text.insert(END,each,i)
  28.     text.insert(END,'\n')
  29.     text.insert(END,'\n')
  30.    
  31. for i, j in zip(range(len(one)), two):
  32.     text.tag_config(i,foreground = 'blue',underline = True)
  33.     text.tag_bind(i,'<Enter>',show_arrow_cursor)
  34.     text.tag_bind(i,'<Leave>',show_xterm__cursor)
  35.     text.tag_bind(i,'<Button-1>',partial(click, u=j))

  36. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-11 20:15:03 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2022-5-12 11:25 编辑

参考代码:
  1. from tkinter import *
  2. import webbrowser
  3. from functools import partial
  4. one = ['测试一', '测试二', '测试三','测试四', '测试五'] #热榜list

  5. two = [
  6.     'http://www.baidu.com',
  7.     'http://www.qq.com',
  8.     'http://www.zhihu.com',
  9.     'http://www.taobao.com',
  10.     'http://www.bing.com
  11.     ] #热榜对应链接


  12. root = Tk()
  13. root.title('百度贴吧热榜')

  14. sb = Scrollbar(root)

  15. sb.pack(side=RIGHT,fill=Y)

  16. text = Text(root,width=30,height=30,yscrollcommand=sb.set)

  17. text.pack(side=LEFT)

  18. sb.config(command=text.yview)


  19. def show_arrow_cursor(event):
  20.     text.config(cursor = "arrow")

  21. def show_xterm__cursor(event):
  22.     text.config(cursor = "xterm")

  23. def click(event, u):
  24.     webbrowser.open(u)
  25.    
  26. for i,each in enumerate(one):
  27.     text.insert(END,each,i)
  28.     text.insert(END,'\n')
  29.     text.insert(END,'\n')
  30.    
  31. for i, j in zip(range(len(one)), two):
  32.     text.tag_config(i,foreground = 'blue',underline = True)
  33.     text.tag_bind(i,'<Enter>',show_arrow_cursor)
  34.     text.tag_bind(i,'<Leave>',show_xterm__cursor)
  35.     text.tag_bind(i,'<Button-1>',partial(click, u=j))

  36. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-11 22:12:04 | 显示全部楼层

在你的代码上做了点改动,参考代码:

  1. from tkinter import *
  2. import webbrowser


  3. url_dict = {
  4.     '百度': 'http://www.baidu.com',
  5.     '必应': 'https://cn.bing.com/',
  6.     '鱼C论坛': 'https://fishc.com.cn/'
  7. }

  8. root = Tk()
  9. root.title('百度贴吧热榜')

  10. sb = Scrollbar(root)

  11. sb.pack(side=RIGHT, fill=Y)

  12. text = Text(root, width=30, height=30, yscrollcommand=sb.set)

  13. text.pack(side=LEFT)

  14. sb.config(command=text.yview)


  15. def show_arrow_cursor(event):
  16.     text.config(cursor="arrow")


  17. def show_xterm__cursor(event):
  18.     text.config(cursor="xterm")



  19. def click(event):
  20.     row = text.index("current").split(".")[0]
  21.     webbrowser.open(url_line[row])

  22. index = 1
  23. url_line = {}
  24. for i in url_dict:
  25.     text.insert(END, i)
  26.     text.tag_add(f"link{index}", f"{index}.0", f'{index}.{len(i)}')
  27.     url_line[str(index)] = url_dict[i]
  28.     text.insert(END, '\n')
  29.     text.insert(END, '\n')

  30.     text.tag_config(f"link{index}", foreground='blue', underline=True)
  31.     text.tag_bind(f"link{index}", '<Enter>', show_arrow_cursor)
  32.     text.tag_bind(f"link{index}", '<Leave>', show_xterm__cursor)

  33.     text.tag_bind(f"link{index}", '<Button-1>', click)

  34.     index += 2


  35. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-12 10:45:59 | 显示全部楼层

谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-12 10:46:53 | 显示全部楼层
Twilight6 发表于 2022-5-11 22:12
在你的代码上做了点改动,参考代码:

大佬这个写法更复杂,我不好理解
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-12 10:50:12 | 显示全部楼层
bhunht 发表于 2022-5-12 10:46
大佬这个写法更复杂,我不好理解



没事,我写的确实麻烦了不少

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 04:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表