row - pySimpleGUI:一种在每行或每个单元格的基础上为表格构建工具提示的方法?
问题描述
我试图让我的表格的最后一列可读。已经为封装表格的列元素而苦苦挣扎,以便在我的表格上显示水平滚动条(嵌套在选项卡中)。但这并没有真正的帮助,因为我无法获得正确宽度的最后一列宽度。(整个表是通过 csv 提取填充的。)所以我想我可以通过工具提示来处理这个问题。我设法用从第一行提取的数据为整个表更新了一个,但这显然不是我需要的。至少,我需要在每一行上迭代构建工具提示。任何想法 ?附属问题:阅读 pySimplegui 文档,它谈到了 textwrap 但我找不到这个库的文档。任何链接?
在我的代码下面,希望它不是太脏我不流利......
代码 :
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
解决方案
行的工具提示可能不适合你,它让事情变得更复杂,如何使用额外的元素来显示你的长列。
此处的示例,从网站访问的数据,可能需要一点时间来启动 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()
推荐阅读
- powershell - PowerShell .NET Core 找不到类型 Windows.Storage.StorageFile
- csv - 将 CSV 字符集从 Shift-JIS 转换为 UTF-8
- php - 用其他列关闭然后打开的情况下 MySQL
- flutter - 应用小部件未生成路线 | 火力基地注册
- javascript - 如何在我的 ReactJS 应用程序中包含外部脚本?
- strftime - strftime 的两个不同的联机帮助页?
- python - 为循环目的组合月-年列
- javascript - 一元运算符语法错误提示“必须使用括号来消除运算符优先级的歧义”
- java - 如何将整数数组传递给java中的队列接口
- atom-editor - hunspell 接受以 s 结尾的词的所有格(s 后的尾撇号)