Pysimplegui Not responding when running with long function

  pysimplegui, python, python-3.x

Is there any way to make a long function run in PysimpleGUI so that it doesn’t become not responsive

for example below when button 1 is clicked after few seconds the window becomes not responsive until function is completed during this the user cannot click button 2

import time
import PySimpleGUI as sg




def verylongfunct():
    time.sleep(30)
    print('You pressed the button')
    sg.popup_notify("hello")


layout = [
    [sg.Text('This is a very basic PySimpleGUI layout')],
    [sg.Input()],
    [sg.Button('Button'),sg.Button('Button2'), sg.Button('Exit')]
]


window = sg.Window('My new window', layout, grab_anywhere=True)


while True:

    event, values = window.read()
    print(event, values)
  
    if event in [sg.WIN_CLOSED, 'Exit']:
        break
    if event == 'Button':
        verylongfunct()
    elif event == "Button2":
        print("hello")

window.close()
 

Source: Python-3x Questions

4 thoughts on - Pysimplegui Not responding when running with long function

    • There is a new capability, just added in the upcoming 4.46.0 release. You’ll have to get 4.45.0.48 or some other recent version from GitHub to use it. It should be on PyPI by Aug 9th.

      It will start a thread for you and then provide an event when it’s completed.

      I’m going to make your example a little bit longer so that the print output is in the window and we can see what’s going on that way.
      “`python
      import time
      import PySimpleGUI as sg

      def verylongfunct():
      time.sleep(20)

      layout = [
      [sg.Text(‘This is a very basic PySimpleGUI layout’)],
      [sg.Input()],
      [sg.MLine(s=(60 ,10), reroute_stdout=True, write_only=True, autoscroll=True)],
      [sg.Button(‘Button’), sg.Button(‘Button2’), sg.Button(‘Exit’)]
      ]

      window = sg.Window(‘My new window’, layout, grab_anywhere=True)

      while True:

      event, values = window.read()
      print(event, values)

      if event in [sg.WIN_CLOSED, ‘Exit’]:
      break
      if event == ‘Button’:
      print(‘Going to perform the long operation now…’)
      window.perform_long_operation(verylongfunct, ‘-FUNC DONE-‘)
      elif event == “Button2”:
      print(“hello”)
      elif event == ‘-FUNC DONE-‘:
      print(‘You pressed the button’)
      sg.popup(“hello! Your long function has completed”)

      window.close()
      “`

      This link is a GIF that will show you what happens when you run the code.
      https://user-images.githubusercontent.com/46163555/128635195-fdb19ae8-52f3-48a1-98c3-c63602e0f6a5.gif

      NOTE – your function cannot call PySimpleGUI functions. Also, because I’ve re-routed stdout, it can’t do prints. If you remove that multiline, you can print from your function.

  • A new Window method was added to PySimpleGUI 4.46.0, which is not yet on PyPI. You can use the version on GitHub 4.45.0.38 to try it out before it’s released in 4.46.0

    What you can use is:
    `window.perform_long_operation`

    It will start a thread for you, return, and then will send you an event when that long operation completes. It’s a way to get the benefits of threading if you don’t yet know enough Python to write the thread code yourself.

    You’ll find a demo program here:
    https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Long_Operations.py

    There are other Demo Programs that show you how to do it with threading that are also in the Demo Programs folder.

LEAVE A COMMENT