Tkinter和超链接 [英] Tkinter and hyperlinks
问题描述
我正在Tkinter中编写GUI,它所要做的就是有一个搜索框,然后将结果输出为超链接.我有很多麻烦.最好的方法是什么?
I am writing a GUI in Tkinter and all it needs to do is have a search box, and then output the results as a hyperlink. I am having lots of trouble. What is the best way to do this?
找到搜索词后,我便可以打开很多网页.但我希望能够显示超链接并让用户选择要打开的超链接.任何帮助表示赞赏.谢谢!
I was able to open up a lot of webpages once the search terms are found. But I want to be able to display the hyperlinks and for the user to choose which to open. Any help is appreciated. Thanks!
到目前为止,这是我的代码:
This is my code so far:
from Tkinter import *
import json
import webbrowser
with open('data.json', 'r') as f:
database = json.loads(f.read())
def goto(event,href):
webbrowser.open_new(href)
def evaluate(event):
res.delete(1.0, END)
search_param = str(entry.get())
for patient_id in database:
if search_param in str(database[patient_id]).lower():
href = "https://website.com/" + str(patient_id)
res.insert(INSERT, href, "link")
res.bind("<Button-1>", goto(event, href))
w = Tk()
Label(w, text="Search:").pack()
entry = Entry(w)
entry.bind("<Return>", evaluate)
entry.pack()
res = Text(w)
res.pack()
w.mainloop()
推荐答案
这是一种方法.它将每个href放在文本小部件中的单独行上,并用唯一的标签名标记该行,每个标签名都绑定到一个单独的动态创建的callback()
事件处理程序函数,该函数间接调用真实事件处理程序.目的是为其提供一些Tkinter通常不传递的额外参数.这些"shim"事件处理程序可以通过在打开Web浏览器作为目标URL之前将其背景暂时变为红色,从而提供有关所选链接的一些视觉反馈.
Here's one way to do. It puts each href on a separate line in the text widget and tags the line with a unique tag name, each of which are bound to a separate, dynamically created, callback()
event handler function which indirectly calls the real event handler. It's purpose is to supply it some extra arguments not normally passed by Tkinter. These "shim" event handlers make it possible to provide some visual feedback on which link was selected by temporarily turning its background to red before opening the web browser with it as the target url.
此default-keyword-argument-value技巧是将额外数据传递到Tkinter事件处理程序的常用技术.在这种情况下,它用于将所需的href
和tag_name
参数传递给它.
This default-keyword-argument-value trick is a commonly used technique to pass extra data to Tkinter event handlers. In this case it's being used to pass the neededhref
andtag_name
arguments to it.
from Tkinter import *
import json
import time
import webbrowser
with open('data.json', 'r') as f:
database = json.loads(f.read())
def goto(event, href, tag_name):
res = event.widget
res.tag_config(tag_name, background='red') # change tag text style
res.update_idletasks() # make sure change is visible
time.sleep(.5) # optional delay to show changed text
print 'opening:', href # comment out
#webbrowser.open_new(href) # uncomment out
res.tag_config(tag_name, background='white') # restore tag text style
res.update_idletasks()
def evaluate(event):
res.delete('1.0', END)
search_param = str(entry.get())
link_count = 0
for patient_id in database:
if search_param in str(database[patient_id]).lower():
href = "https://website.com/" + str(patient_id)
tag_name = "link" + str(link_count) # create unique name for tag
link_count += 1
# shim to call real event handler with extra args
callback = (lambda event, href=href, tag_name=tag_name:
goto(event, href, tag_name))
res.tag_bind(tag_name, "<Button-1>", callback) # just pass function
# (don't call it)
res.insert(INSERT, href+'\n', (tag_name,)) # insert tagged text
w = Tk()
Label(w, text="Search:").pack()
entry = Entry(w)
entry.bind("<Return>", evaluate)
entry.pack()
res = Text(w)
res.pack()
w.mainloop()
这篇关于Tkinter和超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!