python Tkinter已将文本显示为超链接 [英] python Tkinter have displayed text as hyperlink

查看:2334
本文介绍了python Tkinter已将文本显示为超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium和Tkinter为我的大学网站创建一个简单的搜索引擎。我的代码创建了GUI并生成了指向搜索框中输入的查询的URL链接。是否可以将这些生成的URL作为GUI中的可点击超链接?如果是这样,我将如何进行此操作?

I am creating a simple search engine for my university site using Selenium and Tkinter. My code creates the GUI and produces URL links to queries entered into the search box. Is it possible to have these produced URLs as clickable hyperlinks within the GUI? If so, how would I go about doing this?

import selenium.webdriver as webdriver
from selenium.webdriver.chrome.options import Options
from tkinter import *



def get_results(search_term, num_results = 10):
    url = "https://www.startpage.com"
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')  
    chromedriver = "chromedriver.exe"
    args = ["hide_console", ]
    browser = webdriver.Chrome(chromedriver, service_args = args, options = 
options) 
    browser.get(url)
    browser.set_window_position(0, 0)
    browser.set_window_size(0, 0)
    search_box = browser.find_element_by_id("query")
    search_box.send_keys(search_term)
    search_box.submit()
    try:
        links = 
browser.find_elements_by_xpath("//ol[@class='web_regular_results']//h3//a")
    except:
        links = browser.find_elements_by_xpath("//h3//a")
    results = []
    for link in links[:num_results]:
        href = link.get_attribute("href")
        print(href)
        results.append(href)
        results.append(href)
        mlabel2 = Label(mGui, text = href).pack()
    browser.close()

    return results



def search_query():
    mtext = ment.get()
    user_search = get_results(mtext + " site:essex.ac.uk")

    return

response = get_results
mGui = Tk()
ment = StringVar()
mGui.geometry('640x640+0+0')
mGui.title('Essex University Search')
mlabel = Label(mGui, text = 'Essex University Search', font = ("arial", 40, 
"bold"), fg = "steelblue").pack()
mbutton = Button(mGui, text = 'Search', command = search_query, font = 
("arial", 10), fg = 'white', bg = 'steelblue').pack()
mEntry = Entry(mGui, textvariable = ment).pack()


推荐答案

如果你只想做一个整个Label外观和工作就像一个超链接,这很容易。我只是做一个愚蠢的 webbrowser.open 而不是与webdriver交谈,所以这可以在没有任何额外库或配置的情况下运行。

If you just want to make a whole Label look and work like a hyperlink, this is pretty easy. I'll just do a dumb webbrowser.open instead of talking to a webdriver, so this can run without any extra libraries or configuration.

from tkinter import *
import webbrowser

root = Tk()
link = Label(root, text="http://stackoverflow.com", fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", lambda event: webbrowser.open(link.cget("text")))
root.mainloop()

但是如果你想在Label中的文本中间嵌入超链接,那很难。标签的一部分与其他部分不同,没有简单的方法。当你得到< Button-1> 时,你必须手动找出它所在的字符串中的位置。而且我不知道有任何库包装它并使其更容易。

But if you want to embed hyperlinks in the middle of text within a Label, that's hard. There's no easy way to style part of a Label different from the rest. And when you get that <Button-1>, you have to manually figure out where in the string it hit. And I'm not aware of any libraries that wrap this up and make it easier.

如果你愿意使用 Text 而不是 Label ,另一方面,支持已经存在,并且包装起来并不是那么难。借用 超链接管理器 从tkinter书出来:

If you're willing to use a Text instead of Label, on the other hand, the support is already there, and wrapping it up isn't that hard. Borrowing the Hyperlink Manager out of the tkinter book:

from functools import partial
from tkinter import *
import webbrowser
from tkHyperlinkManager import HyperlinkManager

root = Tk()
text = Text()
text.pack()
hyperlink = HyperlinkManager(text)
text.insert(INSERT, "Hello, ")
text.insert(INSERT, "Stack Overflow",
            hyperlink.add(partial(webbrowser.open, "http://stackoverflow.com")))
text.insert(INSERT, "!\n\n")
text.insert(INSERT, "And here's ")
text.insert(INSERT, "a search engine",
            hyperlink.add(partial(webbrowser.open, "http://duckduckgo.com")))
text.insert(INSERT, ".")

root.mainloop()

当然你可以结束部分(webbrowser.open,url)在方法中,所以你只需要调用 hyperlink.add('http://stackoverflow.com'),或者甚至将 text.insert 包装在那里你可以调用 hyperlink.insert(INSERT,'Stack Overflow','http://stackoverflow.com'),但我离开了 tkHyperlinkManager的简单界面单独让它更容易理解它正在做什么。

Of course you could wrap up the partial(webbrowser.open, url) in a method so you can just call hyperlink.add('http://stackoverflow.com'), or even wrap the text.insert in there so you can just call hyperlink.insert(INSERT, 'Stack Overflow', 'http://stackoverflow.com'), but I left the simple interface of tkHyperlinkManager alone to make it easier to understand what it's doing.

你可以从非常短而简单的 tkHyperlinkManager 代码,它所做的并不是那么困难。您只需将每个超链接包装在标签中并插入代替原始文本,然后每次事件都会返回标签,这样您就可以轻松查找要为该特定标签执行的操作。

And as you can from the pretty short and simple tkHyperlinkManager code, what it's doing isn't all that difficult. You just wrap each hyperlink in a tag and insert that in place of the raw text, and you get the tag back with each event, so you can easily look up what you want to do for that specific tag.

这篇关于python Tkinter已将文本显示为超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆