Reputation: 31
I'm trying to have the last column of my table readable. Already struggled with the column element encapsulating the table to have horizontal scroll bar to be displayed on my table (nested in a tab). But it doesn't really help as I can't get the last column width with the right width. (The whole table is populated through csv extraction.)
So I thought I could handle this through tooltips. I managed to update one for the whole table with data extracted from the first row but it is obviously not what I need. I need the tooltip to be built iterally on each row, at least.
Any idea ?
subsidiary question : reading the pySimplegui doc, it talks about textwrap but I can't find doc to this lib. any link ?
here below my code, hope it's not too dirty I'm not fluent...
code :
i =3 #skippin the first lines
for row in range (0, len(window.FindElement('pronos_table').Values)):
el = pdata[i]
if len(el[3]) > 50 :
str_inf = el[3][0:50]+"\r\n"+el[3][50:] #TODO refine this later with textwrap or something
window['pronos_table'].set_tooltip(str_inf) #tried many things here,
else:
window['pronos_table'].set_tooltip(el[3]) #...and here : got only this to work
i = i + 1
Upvotes: 1
Views: 2308
Reputation: 13061
Tooltip for row maybe not work for you, and it let things more complex, how about use an extra element to show your long column.
Example here, data accessed from web site, maybe take a little time to start GUI.
import requests
from bs4 import BeautifulSoup
import PySimpleGUI as sg
url = 'https://medium.com/@EmEmbarty/31-of-the-best-and-most-famous-short-classic-poems-of-all-time-e445986e6df'
response = requests.get(url)
if response.status_code != 200:
print('Failed to access data from website !')
quit()
html = response.text
soup = BeautifulSoup(html, features='html.parser')
headings = ['Title', 'Auther']
data = []
poem_lst = []
poems = []
for tag in soup.findAll(['h1', 'p'])[7:-12]:
name = tag.name
if name == 'h1':
if poem_lst:
poem = '\n'.join(poem_lst[:-1])
data.append([title, auther])
poems.append(poem)
text = tag.a.text
index1 = text.index('“')
index2 = text.index('” by ')
title, auther = text[index1+1:index2], text[index2+5:]
poem_lst = []
elif name == 'p':
poem_lst.append(tag.text)
poem = '\n'.join(poem_lst[:-1])
data.append([title, auther])
poems.append(poem)
# ---------- GUI start from here ---------
sg.theme('DarkBlue')
sg.set_options(font=('Courier New', 12))
col_widths = [max(map(lambda x:len(x[0])+1, data)), max(map(lambda x:len(x[0]), data))+1]
layout = [
[sg.Table(data, headings=headings, enable_events=True,
justification='left', select_mode=sg.TABLE_SELECT_MODE_BROWSE,
auto_size_columns=False, col_widths=col_widths, key='-TABLE-')],
[sg.Multiline('', size=(50, 10),
text_color='white', background_color=sg.theme_background_color(),
key='-MULTILINE-'),],
]
window = sg.Window("Table", layout, finalize=True)
multiline = window['-MULTILINE-']
multiline.expand(expand_x=True)
multiline.Widget.configure(spacing1=3, spacing2=1, spacing3=3)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == '-TABLE-':
selection = values[event][0]
text = poems[selection]
multiline.update(value=text)
window.close()
Upvotes: 2
Reputation: 31
This is runnable code. Can't get better !
#!/usr/bin/env python3
# coding: utf-8
import re
import PySimpleGUI as sg
bigD = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
headings = [f'headings {col:>2d}' for col in range(3)]
vals = [["|"+str(row)+"- "+bigD if col==2 else f'data {row*10+col:>2d}' for col in range(3)] for row in range(50)]
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 16))
table_layout = [[sg.Table(values=vals, headings=headings, num_rows=50,
auto_size_columns=False, key='pronos_table', alternating_row_color='Grey', col_widths=list(map(lambda x:len(x)+1, headings)),
hide_vertical_scroll=True)]]
layout = [[sg.Column(table_layout, scrollable=True, size=(420, 500))]]
window = sg.Window('Sample excel file', layout, finalize=True)
window.bind('<FocusIn>', '+ROW FOCUS+')
window.bind('<Enter>', '+TABLE ENTER+')
window.bind('<Leave>', '+TABLE LEAVE+')
str_inf = 'Select a row'
while True:
event, values = window.read()
#window.enable()
#window.BringToFront()
print(event,values['pronos_table'])
if event == '+TABLE LEAVE+':
if len(values['pronos_table'])!=0:
el = vals[int(str(values['pronos_table'][0])) if len(values['pronos_table']) != 0 else 0]
el[2] = str(el[2]).strip().replace('None','')
str_inf = re.sub("(.{64})", "\\1\r", el[2], 0, re.DOTALL)
#str_inf = "\n".join(textwrap.wrap(el[3], width=64))
print(len(values['pronos_table']))
window['pronos_table'].set_tooltip(str_inf)
window.close()
Upvotes: 0