[英]Can we create a clickable python string that redirects users to a url?
I'm working on a tkinter project and I have used tkinter.scrolledtext
to show the result of a searching operation, and what I want is a way to redirect each searching result in the scrolled text to a specified url. 我正在研究一个
tkinter.scrolledtext
项目,我使用tkinter.scrolledtext
来显示搜索操作的结果,我想要的是一种将滚动文本中的每个搜索结果重定向到指定URL的方法。
my idea was to make the string in the scrolled text clickable and when I click, it will redirect me to the URL that I specify. 我的想法是使滚动文本中的字符串可单击,当我单击时,它会将我重定向到我指定的URL。 Another solution is to add a button, but I have really searched about that but I didn't find a lot of documentation.
另一个解决方案是添加一个按钮,但我真的已经搜索过了,但我没有找到很多文档。
Any ideas, please! 有任何想法,拜托!
If you can use Listbox
instead of ScrolledText
, below is an example: 如果您可以使用
Listbox
而不是ScrolledText
,下面是一个示例:
from tkinter import *
import webbrowser
def on_url_click(event):
selected = event.widget.nearest(event.y) # find the index near the mouse click position
url = event.widget.get(selected)
webbrowser.open(url) # open the URL using default browser
root = Tk()
urllist = Listbox(root, width=80, height=20)
urllist.pack()
urllist.bind('<Button-1>', on_url_click)
# insert some URLs
for url in ('http://www.google.com', 'http://www.reddit.com', 'http://www.stackoverflow.com'):
urllist.insert(END, url)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.