1

I am creating a search engine based on this Youtube tutorial which gives the output of the search result in a sg.Output element. I want each result to be clickable and open in Windows File Explorer with the file selected.

My issues is in a PySimpleGUI output box (sg.Output) I can only seem to have text.

How can I have text with a link attached to run a subprocess like this? My guess is it is something like what was discussed here:

sg.Text('Here', is_link=True, key='link') # then link the key to an event

However, as previously mentioned, if I add anything but text to sg.Output it does not work, i.e., the following does not work:

sg.window.FindElement('-OUTPUT-').Update(sg.Text('Here', is_link=True, key='link')) 
John
  • 11
  • 1
  • 4
  • When update `value` by `sg.Output.update(value=None, visible=None)`, `value` should be a string to replace current contents of the output area, not another element `sg.Text` – Jason Yang Mar 30 '21 at 10:05
  • That's what my question is. How do I put a string with a hyperlink in that field so that I can click on it to perform an action (e.g. subprocess)? – John Mar 31 '21 at 07:49
  • Most of time, only string and no hyperlink in that field. You should manage hyperlinks by yourself. After identified which string clicked, then you should know which hyperlink to be used. For all of those actions, tkinter code will be required and much complex. You may need to set mark for each string for different appearance, like font, underline, size, color, ..., then get index or mark by position of mouse click, and hyperlink, then call `subprocess.Popen` or else. – Jason Yang Mar 31 '21 at 08:38

2 Answers2

5

It will be much complex to enable hyperlink function for sg.Output or sg.Multiline.

Here's simple code to provide hyperlink function for sg.Text, also work for some other elements, by using options enable_events=True and tooltip.

enter image description here

import webbrowser
import PySimpleGUI as sg

urls = {
    'Google':'https://www.google.com',
    'Amazon':'https://www.amazon.com/',
    'NASA'  :'https://www.nasa.gov/',
    'Python':'https://www.python.org/',
}

items = sorted(urls.keys())

sg.theme("DarkBlue")
font = ('Courier New', 16, 'underline')

layout = [[sg.Text(txt, tooltip=urls[txt], enable_events=True, font=font,
    key=f'URL {urls[txt]}')] for txt in items]
window = sg.Window('Hyperlink', layout, size=(250, 150), finalize=True)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event.startswith("URL "):
        url = event.split(' ')[1]
        webbrowser.open(url)
    print(event, values)

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Thanks for the answer but that is not quite what I want to do. I have created a search engine like this on (https://www.youtube.com/watch?v=IWDC9vcBIFQ&ab_channel=IzzyAnalytics) and I want each result of the search engine in the sg.Output to be linkable (using a sub-process to open windows explorer with the file selected as in my question). – John Mar 31 '21 at 07:52
  • For `sg.Output` or `sg.Multiline`, they are not well structured for text shown, so not easy for hyperlink function. You may use element with item list, like `sg.Tree` or `sg.Table`, then you can define open hyperlink with signle or double click on it, then it return an event for you to call subprocess, like `subprocess.Popen`. – Jason Yang Mar 31 '21 at 08:23
0

This took awhile, however I'm sure I'll use it a lot. xD

Additional you can specify the tooltip parameter to show the site it'll go to.

import PySimpleGUI as sg
import webbrowser

layout = [
    [
        sg.Text("My Awesome Link", text_color="#0000EE", font=(None, 20), enable_events=True, key="-LINK-")
    ]
]

window = sg.Window("Hyperlink", layout, finalize=True)

window["-LINK-"].set_cursor("hand2")
window["-LINK-"].Widget.bind("<Enter>", lambda _: window["-LINK-"].update(font=(None, 20, "underline")))
window["-LINK-"].Widget.bind("<Leave>", lambda _: window["-LINK-"].update(font=(None, 20)))

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event == "-LINK-":
        webbrowser.open("https://www.pysimplegui.org/")

window.close()