Clickable hypertext within PySimpleGUI table created from list
Question:
I have a PySimpleGUI table updating from a list. In that list, the first item is a website link. How can I make PySimpleGUI recognise them as clickable links within it’s table? I have the following code which creates the table from a list that is updating periodically, column 1 contains websites that I would like clickable:
import PySimpleGUI as sg
def getData():
#data update code that creates a new list every x secs
return [("https://www.google.com", "Google", "10", "5445", "No"),("https://www.yahoo.com", "yahoo", "2", "333", "Yes")]
dat = getData()
headings = ["Link", "Name", "Users", "Id", "Authorized"]
sg.set_options(dpi_awareness=True)
layout = [
[sg.Table(values=dat, headings=headings, max_col_width=25,
auto_size_columns=True,
enable_events=True,
# display_row_numbers=True,
justification='right',
num_rows=20,
alternating_row_color=sg.theme_button_color()[1],
key='-TABLE-',
# selected_row_colors='red on yellow',
# enable_events=True,
# select_mode=sg.TABLE_SELECT_MODE_BROWSE,
expand_x=True,
expand_y=True,
enable_click_events=True, # Comment out to not enable header and other clicks
)],
]
window = sg.Window('Table Element - Example 1', layout, resizable=True, finalize=True)
while True:
dat = getData() #get new data for table
event, values = window.read(timeout=1000)
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == sg.TIMEOUT_KEY:
window['-TABLE-'].update(values=dat)
window.refresh()
window.close()
Answers:
Use option enable_click_events
to get where you clicked on the Table element.
The format for the event will be: ('-TABLE-', '+CLICKED+', (row, col))
It will mean the column-0 link clicked if row
is not None
, greater or equal then 0, and col
is 0.
Reduced Example Code – There’s no way to change the color or underline for specific cell, so just only for clickable link.
import webbrowser
import PySimpleGUI as sg
data = [
("Link", "Name", "Users", "Id", "Authorized"),
("https://www.google.com", "Google", "10", "5445", "No"),
("https://www.yahoo.com", "yahoo", "2", "333", "Yes"),
]
col_widths = list(map(lambda y:max(y)+2, map(lambda x:map(len, x), zip(*data))))
sg.theme("LightBlue")
sg.set_options(font=("Courier New", 16))
layout = [
[sg.Table(
values=data[1:],
headings=data[0],
auto_size_columns=False,
col_widths=col_widths,
enable_click_events=True,
cols_justification='lcccc',
num_rows=10,
key='-TABLE-',
)],
[sg.Push(), sg.Button("Check")],
]
window = sg.Window('Table', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif isinstance(event, tuple) and event[:-1] == ('-TABLE-', '+CLICKED+'):
row, col = event[-1]
if row is not None and row >= 0 and col==0:
url = data[row+1][0]
webbrowser.open(url)
window.close()
I have a PySimpleGUI table updating from a list. In that list, the first item is a website link. How can I make PySimpleGUI recognise them as clickable links within it’s table? I have the following code which creates the table from a list that is updating periodically, column 1 contains websites that I would like clickable:
import PySimpleGUI as sg
def getData():
#data update code that creates a new list every x secs
return [("https://www.google.com", "Google", "10", "5445", "No"),("https://www.yahoo.com", "yahoo", "2", "333", "Yes")]
dat = getData()
headings = ["Link", "Name", "Users", "Id", "Authorized"]
sg.set_options(dpi_awareness=True)
layout = [
[sg.Table(values=dat, headings=headings, max_col_width=25,
auto_size_columns=True,
enable_events=True,
# display_row_numbers=True,
justification='right',
num_rows=20,
alternating_row_color=sg.theme_button_color()[1],
key='-TABLE-',
# selected_row_colors='red on yellow',
# enable_events=True,
# select_mode=sg.TABLE_SELECT_MODE_BROWSE,
expand_x=True,
expand_y=True,
enable_click_events=True, # Comment out to not enable header and other clicks
)],
]
window = sg.Window('Table Element - Example 1', layout, resizable=True, finalize=True)
while True:
dat = getData() #get new data for table
event, values = window.read(timeout=1000)
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == sg.TIMEOUT_KEY:
window['-TABLE-'].update(values=dat)
window.refresh()
window.close()
Use option enable_click_events
to get where you clicked on the Table element.
The format for the event will be: ('-TABLE-', '+CLICKED+', (row, col))
It will mean the column-0 link clicked if row
is not None
, greater or equal then 0, and col
is 0.
Reduced Example Code – There’s no way to change the color or underline for specific cell, so just only for clickable link.
import webbrowser
import PySimpleGUI as sg
data = [
("Link", "Name", "Users", "Id", "Authorized"),
("https://www.google.com", "Google", "10", "5445", "No"),
("https://www.yahoo.com", "yahoo", "2", "333", "Yes"),
]
col_widths = list(map(lambda y:max(y)+2, map(lambda x:map(len, x), zip(*data))))
sg.theme("LightBlue")
sg.set_options(font=("Courier New", 16))
layout = [
[sg.Table(
values=data[1:],
headings=data[0],
auto_size_columns=False,
col_widths=col_widths,
enable_click_events=True,
cols_justification='lcccc',
num_rows=10,
key='-TABLE-',
)],
[sg.Push(), sg.Button("Check")],
]
window = sg.Window('Table', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif isinstance(event, tuple) and event[:-1] == ('-TABLE-', '+CLICKED+'):
row, col = event[-1]
if row is not None and row >= 0 and col==0:
url = data[row+1][0]
webbrowser.open(url)
window.close()