鱼C论坛

 找回密码
 立即注册
查看: 1863|回复: 11

无法点链接

[复制链接]
发表于 2020-11-18 13:28:13 | 显示全部楼层 |阅读模式

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

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

x
  1. from tkinter import *
  2. root = Tk()
  3. text = Text(root, width=30, height=5)
  4. text.pack()
  5. text.insert(INSERT, "I love FishC.com!")
  6. text.tag_add("link", "1.7", "1.16")
  7. text.tag_config("link", foreground="blue", underline=True)
  8. def show_hand_cursor(event):
  9.         text.config(cursor="arrow")
  10.         def show_arrow_cursor(event):
  11.                 text.config(cursor="xterm")
  12.                 def click(event):
  13.                         webbrowser.open("http://www.fishc.com")
  14.                         text.tag_bind("link", "<Enter>", show_hand_cursor)
  15.                         text.tag_bind("link", "<Leave>", show_arrow_cursor)
  16.                         text.tag_bind("link", "<Button-1>", click)
  17.                         mainloop()
复制代码


为什么点不到链接?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-18 17:09:33 | 显示全部楼层
本帖最后由 笨鸟学飞 于 2020-11-18 17:11 编辑

啥意思?把问题说清楚一点,你这缩进都乱成啥样了。代码能跑成功就有鬼了。
而且你的事件绑定也写的乱七八糟的。都不知道你想干啥。。

建议
1、把自己的代码注释一下你想干啥
2、把问题描述清楚
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2020-11-18 17:23:32 | 显示全部楼层
把你的缩进改了一下,然后我添加了一个import webbrowser。如下:
import webbrowser
from tkinter import *
root = Tk()
text = Text(root, width=30, height=5)
text.pack()
text.insert(INSERT, "I love FishC.com!")
text.tag_add("link", "1.7", "1.16")
text.tag_config("link", foreground="blue", underline=True)
def show_hand_cursor(event):
        text.config(cursor="arrow")
def show_arrow_cursor(event):
        text.config(cursor="xterm")
def click(event):
        webbrowser.open("http://www.fishc.com")
text.tag_bind("link", "<Enter>", show_hand_cursor)
text.tag_bind("link", "<Leave>", show_arrow_cursor)
text.tag_bind("link", "<Button-1>", click)
mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-18 17:25:04 | 显示全部楼层
缩进的问题。我改成正常缩进,可以正常运行。

import webbrowser
from tkinter import *
root = Tk()
text = Text(root, width=30, height=5)
text.pack()
text.insert(INSERT, "I love FishC.com!")
text.tag_add("link", "1.7", "1.16")
text.tag_config("link", foreground="blue", underline=True)
def show_hand_cursor(event):
        text.config(cursor="arrow")
def show_arrow_cursor(event):
        text.config(cursor="xterm")
def click(event):
        webbrowser.open("http://www.fishc.com")
text.tag_bind("link", "<Enter>", show_hand_cursor)
text.tag_bind("link", "<Leave>", show_arrow_cursor)
text.tag_bind("link", "<Button-1>", click)
mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-18 18:51:44 | 显示全部楼层
缩进的问题。
def show_hand_cursor(event):
        text.config(cursor="arrow")
def show_arrow_cursor(event):
        text.config(cursor="xterm")
def click(event):
        webbrowser.open("http://www.fishc.com")
        text.tag_bind("link", "<Enter>", show_hand_cursor)
        text.tag_bind("link", "<Leave>", show_arrow_cursor)
        text.tag_bind("link", "<Button-1>", click)
mainloop()
就没问题了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-18 18:55:03 | 显示全部楼层
本帖最后由 gonff 于 2020-11-18 19:03 编辑

缩进的问题。把def开头的语句都移到开头,后面跟的也相应的前移。然后mainloop()也要移到行首处。缩进搞定了,光标就正常了。
然后还少了import webbrowser这一句。要不然无法打开网页。

感觉你可能是从别处复制过来的代码,以后要注意缩进。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-18 18:57:26 | 显示全部楼层
  1. import webbrowser
  2. from tkinter import *
  3. root = Tk()
  4. text = Text(root, width=30, height=5)
  5. text.pack()
  6. text.insert(INSERT, "I love FishC.com!")
  7. text.tag_add("link", "1.7", "1.16")
  8. text.tag_config("link", foreground="blue", underline=True)
  9. def show_hand_cursor(event):
  10.         text.config(cursor="arrow")
  11. def show_arrow_cursor(event):
  12.         text.config(cursor="xterm")
  13. def click(event):
  14.         webbrowser.open("http://www.fishc.com")
  15. text.tag_bind("link", "<Enter>", show_hand_cursor)
  16. text.tag_bind("link", "<Leave>", show_arrow_cursor)
  17. text.tag_bind("link", "<Button-1>", click)
  18. mainloop()
复制代码


这个可以执行。也能进网页。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-19 15:12:26 | 显示全部楼层
gonff 发表于 2020-11-18 18:57
这个可以执行。也能进网页。

不好意思,我看书看漏了。为什么增加import webbrowser还是进不了网页?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-19 16:46:57 | 显示全部楼层
可能是要光标变形之后才可以。如果是文本输入形态的光标点了没用。因为这个时候他判定你没有点在link上。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-23 14:30:04 | 显示全部楼层
gonff 发表于 2020-11-19 16:46
可能是要光标变形之后才可以。如果是文本输入形态的光标点了没用。因为这个时候他判定你没有点在link上。

光标点到链接上面不变形。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-23 19:34:08 | 显示全部楼层
  1. import webbrowser
  2. from tkinter import *
  3. root = Tk()
  4. text = Text(root, width=30, height=5)
  5. text.pack()
  6. text.insert(INSERT, "I love FishC.com!")
  7. text.tag_add("link", "1.7", "1.16")
  8. text.tag_config("link", foreground="blue", underline=True)
  9. def show_hand_cursor(event):
  10.     text.config(cursor="arrow")
  11. def show_arrow_cursor(event):
  12.     text.config(cursor="xterm")
  13. def click(event):
  14.     webbrowser.open("http://www.fishc.com")
  15. text.tag_bind("link", "<Enter>", show_hand_cursor)
  16. text.tag_bind("link", "<Leave>", show_arrow_cursor)
  17. text.tag_bind("link", "<Button-1>", click)
  18. mainloop()
复制代码


我直接复制代码,可以正常运行,光标会变化,也能打开网页。不过我发现缩进还有点细微的问题,你再试试这个吧!又改了两个缩进。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-6 21:27:09 | 显示全部楼层
你这是什么缩进。。。而且没导入 webbrowser 模块

帮你改好了:

  1. import webbrowser
  2. from tkinter import *
  3. root = Tk()
  4. text = Text(root, width=30, height=5)
  5. text.pack()
  6. text.insert(INSERT, "I love FishC.com!")
  7. text.tag_add("link", "1.7", "1.16")
  8. text.tag_config("link", foreground="blue", underline=True)
  9. def show_hand_cursor(event):
  10.     text.config(cursor="arrow")
  11. def show_arrow_cursor(event):
  12.     text.config(cursor="xterm")
  13. def click(event):
  14.     webbrowser.open("http://www.fishc.com")
  15. text.tag_bind("link", "<Enter>", show_hand_cursor)
  16. text.tag_bind("link", "<Leave>", show_arrow_cursor)
  17. text.tag_bind("link", "<Button-1>", click)
  18. mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 18:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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