首页 > 解决方案 > 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 

标签: rowtooltippysimplegui

解决方案


行的工具提示可能不适合你,它让事情变得更复杂,如何使用额外的元素来显示你的长列。

此处的示例,从网站访问的数据,可能需要一点时间来启动 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()

推荐阅读