简体   繁体   English

我们可以创建一个可点击的python字符串,将用户重定向到网址吗?

[英]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.

相关问题 我们可以在 python 中向终端/控制台添加可点击的文本或按钮吗? - Can we add clickable text or button to terminal/console in python? 如何在云端硬盘中创建重定向到特定网址的快捷方式 - How can I create a shortcut in Drive that redirects to a specific url Python:我们可以在字符串中断言“in”和“not in”吗? - Python: Can we assert "in" and "not in" in a string? 在 python 中创建一个可点击的网格 - Create a clickable grid in python 如何从URL查询字符串中找不到空变量? 或者我们可以从python中的请求获取URL查询字符串中的变量 - How to find not null variables from URL query string? or can we get the variable in URL query string from request in python 我们可以使用python中的pandas dataframe将索引称为用户输入 - can we call an index as an users input using pandas dataframe in python 我们可以用django创建带有扭曲python的应用程序吗? - Can we create apps with twisted python as django…? 我们可以在 streamlit [Python] 中创建形状吗 - Can we create shapes in streamlit [Python] 我们可以使用Python脚本创建注释吗? - Can we use to Python script to create a comment? 我们可以使用Python在Rabbitmq中创建队列吗 - Can we create queue in rabbitmq with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM