This question already has an answer here:
- How to create a hyperlink with a Label in Tkinter? 2 answers
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()