Issue
I'm using PySimpleGUI and Python to create an xslt-file based on input from csv-file and xml-schemafile (xsd). The idea is to provide the user with a GUI to select csv-header and map to xml element/tag. I've chosen a layout where the user creates the rows needed since the number of rows is up to the user. The xslt-file is then used to transform to xml-file that validates to a schema.
The problem I have is that the xslt is not working as transformer if input fields contains empty values or "''". So I'm looking for a way to omit input from output, to point out that "this row with input is passed to the dict output but not this row".
Below is my experiment with making rows invisible and adding a checkbox to check which rows to select as output in "print(values)" in the else-statement (without all the csv/xml stuff; #event == '-CREATEXSLT-' is the method I use to create dict that outputs xslt). Just making row invisible doesn't work because it is not deleted (according to my research you can't delete, only hide). I'm very open to any solution based on PySimpleGUI and Python. I've tried to make "print(values)" to print only dict with with key:values from visible rows and from "visible=True" checkbox, but no luck.
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()
Code is based on: https://github.com/amithr/PySimpleGUI-Dynamically-Add-Elements/blob/main/main.py Screen dump of GUI
Solution
Ok... dict comprehension is working for me, creating new dict without the unwanted key value-pairs, checkbox not needed:
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()
Answered By - Martin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.