Search code examples
pythonuser-interfacepysimplegui

PySimpleGUI not responding


Im using PySimpleGui to make a simple file format conversion program, but the little window of my program keeps telling me (not responding) like if it was crushing while in reality it's working and its writing the new file.

The issue is the cycle, if i remove it everything works but the user doesnt have any response on the progression of the conversion. I reed some documentation on python threading and i think that everything should work, any tips? here's the code: `

def main():
    
    
    sg.theme('DarkGrey3')
    layout1 = [[sg.Text('File converter (.csv to sdf)')],
              [sg.Frame('Input Filename', 
                        [[sg.Input(key='-IN-'), sg.FileBrowse(), ],])],
              [sg.Frame('Output Path', 
                        [[sg.Input(key='-OUT-'), sg.FolderBrowse(), ],])],
              [sg.Button('Convert'), sg.Button('Exit')], [sg.Text('', key='-c-')]]
    window=sg.Window(title='.csv to .sdf file converter', layout=layout1, margins=(50, 25))
    window.read()
    while True:
        event, values = window.read()
        if event=='Exit' or event==None:
            break
        if event=='Convert':            
            csvfilepath=values['-IN-']
            outpath=values['-OUT-']
            x=threading.Thread(target=Converter, args=[csvfilepath, outpath])
            x.start()
            time.sleep(1)
            while x.is_alive():
                window['-c-'].Update('Conversion')
                time.sleep(1)
                window['-c-'].Update('Conversion.')
                time.sleep(1)
                window['-c-'].Update('Conversion..')
                time.sleep(1)
                window['-c-'].Update('Conversion...')
                time.sleep(1)

`


Solution

  • Following code demo how to update GUI by timeout event and threading.

    import time
    import threading
    import PySimpleGUI as sg
    
    def convert_func(window):
        for i in range(10):    # Simuate the conversion
            window.write_event_value('Conversion Step', i)
            time.sleep(1)
        window.write_event_value('Conversion Done', None)
    
    layout = [
        [sg.Button('Convert'), sg.Text('', expand_x=True, key='Step')],
        [sg.StatusBar('', size=20, key='Status')],
    ]
    window = sg.Window('Title', layout, finalize=True)
    status, step = window['Status'], window['Step']
    running, index, m = False, 0, 10
    msg = ['Conversion'+'.'*i for i in range(m)]
    
    while True:
    
        event, values = window.read(timeout=200)
    
        if event == sg.WIN_CLOSED:
            break
        elif event == 'Convert' and not running:
            threading.Thread(target=convert_func, args=(window,), daemon=True).start()
            running = True
        elif event == sg.TIMEOUT_EVENT and running:
            status.update(msg[index])
            index = (index + 1) % m
        elif event == 'Conversion Step':
            print(event)
            i = values[event]
            step.update(f'Step {values[event]}')
        elif event == 'Conversion Done':
            running = False
            step.update('')
            status.update(event)
    
    window.close()
    

    enter image description here