Creating independent GUIs using Python Multiprocessing module allows you to run multiple GUI windows concurrently without blocking the main thread. This can be useful when you want to create separate interfaces for different tasks, or when you want to display real-time updates in one window while performing computations in another.
In this tutorial, we will use PySimpleGUI to create the GUI windows and the Multiprocessing module to run them concurrently. PySimpleGUI is a simple yet powerful GUI framework for Python that allows you to create custom GUIs with ease.
Step 1: Install PySimpleGUI
First, you need to install PySimpleGUI by running the following command in your terminal:
pip install PySimpleGUI
Step 2: Import the necessary modules
Next, import the required modules in your Python script:
import PySimpleGUI as sg
import multiprocessing
Step 3: Define the functions for creating GUI windows
Next, define functions that will create the GUI windows. Each function should define the layout of the window and handle event callbacks:
def create_window1():
layout = [[sg.Text('Window 1')],
[sg.Button('Button 1')]]
window = sg.Window('Window 1', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
def create_window2():
layout = [[sg.Text('Window 2')],
[sg.Button('Button 2')]]
window = sg.Window('Window 2', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
Step 4: Run the GUI windows concurrently using Multiprocessing
Now, create a new process for each GUI window using the Multiprocessing module:
if __name__ == '__main__':
p1 = multiprocessing.Process(target=create_window1)
p2 = multiprocessing.Process(target=create_window2)
p1.start()
p2.start()
p1.join()
p2.join()
You can run this script and you will see two separate GUI windows appear on your screen. You can interact with each window independently without blocking each other.
In summary, using the Multiprocessing module in Python allows you to create independent GUI windows that run concurrently without blocking the main thread. This can be useful for creating multi-tasking applications or displaying real-time updates in separate windows.PySimpleGUI simplifies the process of creating GUI windows, making it easy to design custom interfaces for your applications.
The multi processor concept is so useful. Plan to use it in my projects. Thanks for the detailed explanation. 👍
Great Video, it works as advertised when starting all processes at the same time, however, in real life the background window will call (in a menu style) the other windows like pop-ups .. can you explain that ? Thanks
Another couple of quick questions,
you have started 2 processes in this example, is is possible to start more, say 5 to 10 or even more (within limits of course)?
and
can each process call a common definition ?
Thanks again.
Thanks for a great video.
Just a point, I kept getting a runtime error with window.maximize().
I think that is because the line the starts with
sg.Window('Microprocessing Example', …
in the function newwindow is cut off on the video.
The end of that line, I believe, should be
… size=(1200, 900), finalize=True)
Thank you. Good video!
Excellently timed video (for me) as I'm just getting started with PySimpleGui and chipping away at the main questions (theme styling, multi threaded/processes etc).
Have you got this code somewhere? I know it's trivial code but I'd love to see what's in that window args for the on-hovers..?