python-3.x 如何输出PySimpleGUI输入的选择,通过复选框,不可见行或其他东西过滤

9w11ddsr  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(390)

我使用PySimpleGUI和Python来创建一个基于csv文件和xml-schemafile(xsd)输入的xslt文件。这个想法是为用户提供一个GUI来选择csv头并Map到xml元素/标签。我选择了一个用户创建所需行的布局,因为行数取决于用户。然后使用xslt文件转换为验证模式的xml文件。
我遇到的问题是,如果输入字段包含空值或“",xslt就不能作为Transformer工作。所以我正在寻找一种方法来从输出中忽略输入,指出“这一行的输入被传递给dict输出,而不是这一行”。
下面是我的一个实验,我将行设置为不可见,并在else语句的“print(values)”中添加一个复选框来检查选择哪些行作为输出(没有所有的CSV/XML的东西;#event == '-CREATEX-'是我用来创建输出xslt的dict的方法。仅仅使行不可见是行不通的,因为它没有被删除(根据我的研究,你不能删除,只能隐藏)。我对任何基于PySimpleGUI和Python的解决方案都持开放态度。我试过让“print(values)”只打印带有key的dict:values从可见行和“visible=True”复选框,但没有运气。

import PySimpleGUI as sg

def create_row(row_counter, row_number_view):
    row =  [sg.pin(
        sg.Col([[
            sg.Button("X", border_width=0, button_color=(sg.theme_text_color(), sg.theme_background_color()), key=('-DEL-', row_counter)),
            # Combo values represent input from headers from csvfile and elements from xsd-schemafile
            sg.Combo(['INFone', 'INFtwo', 'INFthree'], key='-ERRAND_INFILE-'),
            sg.Combo(['SCHone', 'SCHtwo', 'SCHthree'], key='-ERRAND_SCHEMA-'),
            sg.Checkbox('Pass as output:', default=False, key='-OUT-'),
            ]],
        key=('-ROW-', row_counter), visible=True
        ))]
    return row

layout = [  [sg.Text('Experimenting with GUI input to output via dict to xsltfile', font='15')],
            [sg.Column([create_row(0, 1)], k='-ROW_PANEL-')],
            [sg.Submit('Create', key='-CREATEXSLT-', size=10, button_color='black on pink'), sg.Text('', size=10),
            sg.Button('+Row', enable_events=True, k='-ADD_ITEM-', tooltip='Add new row'),
            sg.Button("Exit", enable_events=True, key='-EXIT-', tooltip='Close window')],
            [sg.Output(size=(65,5), key='output', pad=5, background_color= 'pink', echo_stdout_stderr=True)],
            ]

window = sg.Window('test', 
    layout,  use_default_focus=False, font='15')

row_counter = 0
row_number_view = 1
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == '-EXIT-':
        break
        
    elif event == '-ADD_ITEM-':
        row_counter += 1
        row_number_view += 1
        window.extend_layout(window['-ROW_PANEL-'], [create_row(row_counter, row_number_view)])
        
    elif event[0] == '-DEL-':
        row_number_view -= 1
        window[('-ROW-', event[1])].update(visible=False)
        window[('-ROW-', event[1])].Widget.pack_forget()
        #window[('-ROW-', event[1])].Widget.destroy()
        
    else:
        #event == '-CREATEXSLT-'
        print(values)
            
window.close()

字符串
代码基于:https://github.com/amithr/PySimpleGUI-Dynamically-Add-Elements/blob/main/main.pyScreen dump of GUI

lskq00tm

lskq00tm1#

好的. dict理解对我来说是有效的,创建新的dict而不需要不需要的键值对,不需要复选框:

import PySimpleGUI as sg

def create_row(row_counter, row_number_view):
    row =  [sg.pin(
        sg.Col([[
            sg.Button("X", border_width=0, button_color=(sg.theme_text_color(), sg.theme_background_color()), key=('-DEL-', row_counter)),
            sg.Combo(['INFone', 'INFtwo', 'INFthree'], key='-ERRAND_INFILE-'),
            sg.Combo(['SCHone', 'SCHtwo', 'SCHthree'], key='-ERRAND_SCHEMA-'),
            ]],
        key=('-ROW-', row_counter), visible=True
        ))]
    return row

layout = [  [sg.Text('Experimenting with GUI input to output via dict to xsltfile', font='15')],
            [sg.Column([create_row(0, 1)], k='-ROW_PANEL-')],
            [sg.Submit('Create', key='-CREATEXSLT-', size=10, button_color='black on pink'), sg.Text('', size=10),
            sg.Button('+Row', enable_events=True, k='-ADD_ITEM-', tooltip='Add new row'),
            sg.Button("Exit", enable_events=True, key='-EXIT-', tooltip='Close window')],
            [sg.Output(size=(65,5), key='output', pad=5, background_color= 'pink', echo_stdout_stderr=True)],
            ]

window = sg.Window('test', 
    layout,  use_default_focus=False, font='15')

row_counter = 0
row_number_view = 1
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == '-EXIT-':
        break
        
    elif event == '-ADD_ITEM-':
        row_counter += 1
        row_number_view += 1
        window.extend_layout(window['-ROW_PANEL-'], [create_row(row_counter, row_number_view)])
        
    elif event[0] == '-DEL-':
        row_number_view -= 1
        window[('-ROW-', event[1])].update(visible=False)
        window[('-ROW-', event[1])].Widget.pack_forget()
        window[('-ROW-', event[1])].Widget.destroy()
        
    else:
        # Dict comprehension before print. Not sure solution for Exception is good.
        values = {k:v for k,v in values.items() if v != ''}
        values = {k:v for k,v in values.items() if v != '*Exception occurred*'}
        print(values)

window.close()

字符串

相关问题