PySimpleGUI – faster GUI creation

PySimpleGUI is a wrapper that facilitates and speeds up the creation of Python window applications. There are 4 ports, based on the following libraries: tkinter, Qt, WxPython, Remi.

Changing the port eg. from PySimpleGUI (based on tkinter) to PySimpleGUIQt (using Qt) does not require any further code changes! More details can be found >>here<<.

A sample program that collects data from the user and displays the data in a second window:

import PySimpleGUIQt as sg

layout = [[sg.Text('Please enter some sample text')],
          [sg.InputText()],
          [sg.Submit('Apply'), sg.Cancel('Cancel')]]

window = sg.Window('Data source window', layout)

event, values = window.Read()

window.Close()

if event == 'Apply':
    text_input = values[0]
    sg.Popup('Text entered:', text_input, title='Data display window')

The first line of the script imports the wrapper module (it must be installed in the system e.g. via pip, as well as the framework that is used by the port – in this case Qt).

The next line defines the layout, which is a list of lists. Each subsequent list defines the next line of the template – the Text element on the first line that writes ‘Please enter some sample text’ , the InputText field in the second, and the Apply and Cancel buttons on the third line.

Then we create a window and assign the previously created layout as the second argument. Pressing any button or closing the window will switch to the window status reading – a tuple consisting of: the event element (e.g. button name or None when the window closing button is pressed) and a dictionary containing keys describing the window input fields and the values ​​entered in these fields is returned.

When the key argument is not given for an input field when creating a layout, by default the dictionary keys are consecutive integer values. In the above example, the text entered, which is saved in the values ​​dictionary as the key value 0, is passed as an element of the string displayed in the popup window (Window displaying data).

Window applications in Python and PyQt5 – using UI files

In Python, we can use .ui files describing the appearance of the interface generated in the Qt Designer tool. To run a window application that uses the xml ui file containing the interface appearance and uses the Qt library, install the PyQt5 overlay, i.e.

pip install PyQt5

Then we can use the following code which loads the ui file and sets the title of the application window.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic

class MyApp(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        QMainWindow.setWindowTitle(self, 'App Window Title')
        uic.loadUi('app.ui', self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

The above code does nothing, except to display a window with selected buttons, labels, etc. In order for these elements to respond to e.g. a click, you must define signals and slots for the GUI element e.g. for a button named calculateButton we create a method which will be the slot corresponding to the clicked signal. A list of signals that a given element can respond to can be found in the Qt library documentation. We place the assignment of the method responding to the signal in the init () method, i.e.

self.calculateButton.clicked.connect(self.my_calculate_function)

and then define the method that will be run when the button is pressed (in this case, the pass statement):

def my_calculate_function(self):
        pass