在 PySimpleGUI 中向表添加键值

add key values to a table in pysimplegui

提问人:mw00847 提问时间:4/12/2023 最后编辑:Jason Yangmw00847 更新时间:4/13/2023 访问量:98

问:

使用 PySimpleGUI 搜索 DataFrame 并将值打印为如下所示的键

使用 SG 时。下面的文本工作正常,但我无法根据打印出来的值让顶行对齐

right_col = [[sg.Text('PRODUCT: ', size=(200, 1), key='product',font='Courier 50')],

[sg.Column([[sg.Text('RAW MATERIAL                                                            REF NO.                   %W/W' ),]])],
                            [sg.Text('A '), sg.Text(size=(50, 1), key='raw_a',font='Courier 12'),sg.Text(key='raw_a_ref'),  sg.Text(key='raw_a_%')],
                            [sg.Text('B '), sg.Text(size=(50, 1), key='raw_b',font='Courier 12'),sg.Text(key='raw_b_ref'),  sg.Text(key='raw_b_%')],
                            [sg.Text('C '), sg.Text(size=(50, 1), key='raw_c',font='Courier 12'),sg.Text(key='raw_c_ref'),  sg.Text(key='raw_c_%')],
                            [sg.Text('D '), sg.Text(size=(50, 1), key='raw_d',font='Courier 12'),sg.Text(key='raw_d_ref'),  sg.Text(key='raw_d_%')],
                            [sg.Text('E '), sg.Text(size=(50, 1), key='raw_e',font='Courier 12'),sg.Text(key='raw_e_ref'),  sg.Text(key='raw_e_%')],
                            [sg.Text('F '), sg.Text(size=(50, 1), key='raw_f',font='Courier 12'),sg.Text(key='raw_f_ref'),  sg.Text(key='raw_f_%')],
                            [sg.Text('G '), sg.Text(size=(50, 1), key='raw_g',font='Courier 12'),sg.Text(key='raw_g_ref'),  sg.Text(key='raw_g_%')],
                            [sg.Text('H '), sg.Text(size=(50, 1), key='raw_h',font='Courier 12'),sg.Text(key='raw_h_ref'),  sg.Text(key='raw_h_%')],
                            [sg.Text('I '), sg.Text(size=(50, 1), key='raw_i',font='Courier 12'),sg.Text(key='raw_i_ref'),  sg.Text(key='raw_i_%')],
                            [sg.Text('J '), sg.Text(size=(50, 1), key='raw_j',font='Courier 12'),sg.Text(key='raw_j_ref'),  sg.Text(key='raw_j_%')],
                            [sg.Text('K '), sg.Text(size=(50, 1), key='raw_k',font='Courier 12'),sg.Text(key='raw_k_ref'),  sg.Text(key='raw_k_%')],
                            [sg.Text('L '), sg.Text(size=(50, 1), key='raw_l',font='Courier 12'),sg.Text(key='raw_l_ref'),  sg.Text(key='raw_l_%')],


                            [sg.Text('procedure'),





                            sg.Text(size=(20, 10), key='procedure')]
]

# together
layout = [[sg.Column(left_col, element_justification='c'), sg.VSeperator(), sg.Column(right_col)]]

因此,为了解决对齐问题,我想将键放在表中。但是,无论我以何种方式将它们放在 https://www.tutorialspoint.com/pysimplegui/pysimplegui_table_element.htm 描述的表格中,我都没有成功

我正在解释循环中键的更改,因此我认为它们很容易阅读。

在此处输入图像描述

pysimplegui

评论

0赞 mw00847 4/12/2023
[新加坡。文本('A'), sg.Text(size=(50, 1), key='raw_a',font='Courier 12'),sg.文本(key='raw_a_ref'), sg.文本(key='raw_a_%')]

答:

0赞 Jason Yang 4/13/2023 #1

在标题和行处设置相同的列大小,最好在此处使用相同的元素,以便更好地对齐。sg.Text

示例代码

import PySimpleGUI as sg

headings = ("RAW MATERIAL", "REF NO.", "%W/W")
items = "ABCDEFGHIJKL"
w1, w2, w3 = widths = (50, 20, 20)
left_col = [[sg.Text("Left Column")]]

right_col = [
    [sg.Text('PRODUCT: ', expand_x=True, key='product',font='Courier 50')],
    [sg.Text(heading, size=width) for heading, width in zip(headings, widths)]] + [
    [sg.Text(item, size=w1, key=f'raw_{item.lower()}'),
     sg.Text(f"row_{item.lower()}_ref",  size=w2, key=f"raw_{item.lower()}_ref"),
     sg.Text(f"raw_{item.lower()}_%",size=w3, key=f"raw_{item.lower()}_%")]
        for item in items] + [
    [sg.Text('procedure'), sg.Text(size=(20, 10), key='procedure')],
]

# together
layout = [[sg.Column(left_col, element_justification='c'), sg.VSeperator(), sg.Column(right_col)]]
sg.Window("Title", layout).read(close=True)

enter image description here