PySimpleGUI is a pure Python GUI framework. It can be used to quickly implement GUI programs with 50% to 90% less code than other frameworks, making it a great choice if you are new to Python and want to write your first piece of software quickly.

PIP Install PySimpleGUI

The official documentation pysimplegui. Readthedocs. IO/en/latest /

Advantages of using PySimpleGUI:

  • Create interfaces and operations consistent with using Tkinter, Qt, WxPython, and Remi.
  • The same functionality requires 10 to 50 percent less code than other frameworks.
  • There is no callback function.
  • Controls that access almost all of the underlying GUI frameworks.
  • Support for both PySide2 and PyQt5 (limited support)
  • You can easily switch between Tkinter, Qt, WxPython, and Web (Remi) by changing only the import statement.
  • The only way to write both a desktop – and web-based GUI in Python
  • From scratch to a pure Python implementation with a Python-friendly interface.
  • It appeals to both Python novices and experts.
  • More than 170 demo programs, teach you to integrate popular software packages, such as OpenCV, Matplotlib, PyGame, etc.
  • 200 pages of documentation, a cookbook, lots of built-in docstring help.

A simple example:

Import PySimpleGUI as sg # Sg. theme('DarkAmber') # Set current theme # Define window layout = [[sg.Text('Some Text on Row 1')], [sg.Text('Enter something on Row 2'), sg.InputText()], [sg.Button('Ok'), Sg. Button('Cancel')]] # create window window window = sg. window (' window Title', layout) event, values = window.read() if event in (None, 'Cancel'): break print('You entered ', values[0]) window.close()Copy the code

In PySimpleGUI, the window layout is arranged from top to bottom in list order, and from left to right in secondary lists.

event, values = window.read()

Executing the window receive message code above will return an (Event, VALUES) tuple.

Event: An event, which can be the press of a button, the clicking of some text, the selection of a list item, etc., or None if the user closes the window directly. By default, only Button can trigger events. Other controls require enable_events=True.

Values: is a dictionary containing the values of all input elements. If the dictionary key is not specified, the system automatically numbers it from 0. For example, if there are two input fields, the value of the first field is VALUES [0] and the value of the second field is values[1].

Two window types

One-shot window

It’s a popup window that collects some data, or prompts for data, and then closes it.

import PySimpleGUI as sg
​
sg.theme('DarkBlue1')
​
layout = [[sg.Text('My one-shot window.')],
          [sg.InputText()],
          [sg.Submit(), sg.Cancel()]]
​
window = sg.Window('Window Title', layout)
​
event, values = window.read()
window.close()
​
text_input = values[0]
sg.popup('You entered', text_input)
Copy the code

Click OK and the window closes automatically.

Persistent Windows

It’s a persistent window. Collecting input values, but not closing the window but keeping it visible, is a way to output information to the user and collect input data. Until the user clicks the exit button or closes the window using X. It’s more like a typical Windows/Mac/Linux program.

import PySimpleGUI as sg sg.theme('DarkAmber') layout = [[sg.Text('Persistent window')], [sg.Input(key='-IN-')], [sg.Button('Read'), sg.Exit()]] window = sg.Window('Window that stays open', layout) while True: Read () print(event, values) if event == sg.WIN_CLOSED or event == 'Exit': break window.close()Copy the code

Window always exists, loop read window information, run result:

Read {'-IN-': '1'}
Read {'-IN-': '2'}
Read {'-IN-': '3'}
Read {'-IN-': '4'}
Read {'-IN-': '5'}
Copy the code

If you find your window color palette ugly, use the following code to change the window theme:

# use in code

sg.theme(‘BluePurple’)

View available topics

Import PySimpleGUI as sg theme_name_list = sg.theme_list() print(theme_name_list) # A list of all available themes Sg.preview_all_look_and_feel_themes () # window to show all themesCopy the code

Some common examples

File browsing

import PySimpleGUI as sg
​
sg.theme('Dark Red')
​
layout = [[sg.Text('Browse to a file')],
          [sg.Input(key='-FILE-', visible=False, enable_events=True), sg.FileBrowse()]]
​
event, values = sg.Window('File Compare', layout).read(close=True)
​
print(f'You chose: {values["-FILE-"]}')
Copy the code

Simple data entry window:

A drop-down box

Import PySimpleGUI as SG Layout = [[sg.Combo([' selectable ', 'selectable ',' selectable ', 'selectable '], default_Value =' selectable ', size=(10, 1))], [sg.ok ()]] window = sg.window (' dropdown elements ', layout) while True: event, values = window.read() if event == sg.WIN_CLOSED or event == 'Exit': break print(event, values) window.close()Copy the code

A simple progress bar:

Import PySimpleGUI as SG sg.theme('Dark Blue 8') for I in range(1000): sg.OneLineProgressMeter('One Line Meter Example', i + 1, 1000, 'key')Copy the code

More and more cases, you can view the official demo document: pysimplegui. Readthedocs. IO/en/latest/c…

Learning Python is all about not reinventing the wheel. In the beginning, if you want to quickly create your OWN GUI programs, copy the examples you need in documentation, debug and modify the final interface you want, study their design patterns and logic, and eventually you’ll be able to write great software.

If you find it useful, please like and share!