Python tkinter:Browser 不点击文本自动打开
Python tkinter:Browser opens automatically without clicking the text
我正在使用 python 和 tkinter 开发一个项目,其中 文本小部件 中的内容
根据我的文本小部件中的 program.Now 动态变化我在单击它们时添加了许多 urls.And Web 浏览器 应该 打开。
http://effbot.org/zone/tkinter-text-hyperlink.htm 我参考这个在我的 code.But 中创建超链接,现在我的问题是 浏览器在文本小部件上显示 url 时打开,甚至没有点击在上面.
这是我的代码。
def click1(self,url):
webbrowser.open(url)
restitle,resthumb,resurl=printit()#title,thumbnailimage,url array
while restitle:
raw_data = urllib.request.urlopen(resthumb[0]).read()
tim = PIL.Image.open(io.BytesIO(raw_data))
tim = tim.resize((100, 100), PIL.Image.ANTIALIAS)
timage = ImageTk.PhotoImage(tim)
Lb1.image_create(END, image=timage)
curl=resurl[0]
Lb1.update()
images.append(timage)
Lb1.insert(END,restitle,hyperlink.add(click1(resurl[0])))#Passing url
Lb1.insert(END,"\n")
restitle.pop(0)
resurl.pop(0)
resthumb.pop(0)
我的问题:
我需要知道如何仅在 单击 特定文本(超链接)时打开浏览器,而不是在将它们添加到文本时 widget.I 是 python 的新手对于代码中的任何错误,tkinter 提前表示歉意 snippet.Thanks。
这里的问题是,您不能将函数直接传递给此方法 hyperlink.add
,因为您使用参数调用它,所以它会被执行。 (请注意,在您 link 中显示的示例中,我们仅传递函数的引用而不调用它)。
如果所有行的参数都相同,您可以使用 lambda 表达式,但 url 每次都需要更改,因此,您需要使用类似 partial
的工具:
from functools import partial
[...]
Lb1.insert(END,restitle,hyperlink.add(partial(click1, resurl[0])))
我正在使用 python 和 tkinter 开发一个项目,其中 文本小部件 中的内容 根据我的文本小部件中的 program.Now 动态变化我在单击它们时添加了许多 urls.And Web 浏览器 应该 打开。
http://effbot.org/zone/tkinter-text-hyperlink.htm 我参考这个在我的 code.But 中创建超链接,现在我的问题是 浏览器在文本小部件上显示 url 时打开,甚至没有点击在上面.
这是我的代码。
def click1(self,url):
webbrowser.open(url)
restitle,resthumb,resurl=printit()#title,thumbnailimage,url array
while restitle:
raw_data = urllib.request.urlopen(resthumb[0]).read()
tim = PIL.Image.open(io.BytesIO(raw_data))
tim = tim.resize((100, 100), PIL.Image.ANTIALIAS)
timage = ImageTk.PhotoImage(tim)
Lb1.image_create(END, image=timage)
curl=resurl[0]
Lb1.update()
images.append(timage)
Lb1.insert(END,restitle,hyperlink.add(click1(resurl[0])))#Passing url
Lb1.insert(END,"\n")
restitle.pop(0)
resurl.pop(0)
resthumb.pop(0)
我的问题: 我需要知道如何仅在 单击 特定文本(超链接)时打开浏览器,而不是在将它们添加到文本时 widget.I 是 python 的新手对于代码中的任何错误,tkinter 提前表示歉意 snippet.Thanks。
这里的问题是,您不能将函数直接传递给此方法 hyperlink.add
,因为您使用参数调用它,所以它会被执行。 (请注意,在您 link 中显示的示例中,我们仅传递函数的引用而不调用它)。
如果所有行的参数都相同,您可以使用 lambda 表达式,但 url 每次都需要更改,因此,您需要使用类似 partial
的工具:
from functools import partial
[...]
Lb1.insert(END,restitle,hyperlink.add(partial(click1, resurl[0])))