5

I am using Pysimplegui to output some text in a for loop after doing an event. However, I can only see the all the text at once when the for loop ends. I cannot see it when the for loop is running. The gui shows a "not responding" (but it is running). Any idea of how to fix this?

This seems to happen when i have a for loop and in the for loop the event running takes time to execute. Below is my code.

import PySimpleGUI as sg
import time

def excecutetest(command):
        for i in range(5):
            print (command + str(i))
            time.sleep(2)

layout = [      
    [sg.Text('Information:', size=(40, 1))],      
    [sg.Output(size=(88, 20))],      
    [sg.Text('Input:', size=(15, 1)), sg.InputText(focus=True), sg.Button('Run', bind_return_key=True)],
    [sg.Button('EXIT')]      
        ] 

window = sg.Window('testing', layout)      

# ---===--- Loop taking in user input and using it to call scripts --- #      

while True:      
  (event, value) = window.Read()      
  if event == 'EXIT'  or event is None:      
      break # exit button clicked      
  if event == 'Run':      
      excecutetest(value[0])
window.Close()

I would like the the window to output each for loop live. Thank you.

Zabjaku
  • 97
  • 1
  • 7

1 Answers1

5

You need to add a call to Window.Refresh() if you want something to show up prior to the next call to Window.Read(). It's discussed in the docs and a number of demo programs.

I know it's test code, but it's not recommended to put sleeps into your event loop.

Mike from PSG
  • 5,312
  • 21
  • 39