0

I'm trying to create a simple GUI, in which part of the functionality involves allowing the user to point to a file for loading. After that, I need to check it's length and read it etc, but I'm getting issues with the application freezing at a seemingly very basic step.

I'm whittled it down to the most simple repeatable example I can find below. Click on the submit seems to work, but if you click on the 'Load File' button it freezes.

Can someone please help explain what I'm missing in my understanding of this framework?

import PySimpleGUI as sg

layout = [
    [sg.Input(key='-FILENAME-', visible=False, enable_events=True), sg.FileBrowse('Load File')],
    [sg.Submit(), sg.Cancel(), sg.Debug()],
    [sg.Output(size=(88, 20))]
]
window = sg.Window('Debug Example', layout)

while True: 
    event, values = window.read()
    print(event)
    if event == '-FILENAME-':
        print('File loaded')
    elif event in (None, 'Exit', 'Cancel'):
        break

window.close()
Mark Burgoyne
  • 1,524
  • 4
  • 18
  • 31
  • put the code for loading inside a thread – Narcisse Doudieu Siewe May 16 '20 at 23:58
  • Thanks - can you help me understand why please? I would understand if the code took a long time, but the above code simply prints one line.... why is threading required? – Mark Burgoyne May 17 '20 at 00:21
  • long running process in the same thread as the thread of a gui cause the gui to not update itself, no redrawing, no event catched etc! gui are redrawn at the frequency of your screen they can be 30 images/s so when a long running process took place, nothing can be redrawn an the gui freeze! that why you need to put a long running code in another thread. – Narcisse Doudieu Siewe May 17 '20 at 04:53

1 Answers1

0

Update: It turns out that a simple timeout definition was all that was required. The below code snippet replacing the one from above doing the trick

event, values = window.Read(timeout=100)
Mark Burgoyne
  • 1,524
  • 4
  • 18
  • 31