Save PySimpleGUI and Support Open Source!
How to Create Amazing GUIs with a Library that Needs Your Support
When I started to learn Python, I wanted to build graphical tools that anyone could use and that were easy to build. I tried my hand at Tkinter, but, even though it's part of the Standard Library, it wasn't intuitive or Pythonic. I tried web development, but the learning curve was steep.
So, when I first discovered PySimpleGUI, I felt like I'd finally found a library that even a beginner could use to build a GUI. In fact, the first application that I ever shared with others at work had a PySimpleGUI front end. I've been so happy with working with this library that I'd planned to write a straightforward blog post about PySimpleGUI.
Yet, while preparing to write this post, I visited the PySimpleGUI home page only to learn that this project needs sponsors to survive. The creator of PySimpleGUI is planning to close the project in March 2022, if he cannot secure enough backing to make the project sustainable.
I'd like to offer the demo below to show how easy PySimpleGUI is to use, and to illustrate why you should give it your support at:
github.com/sponsors/PySimpleGUI
Getting Started
In the example in this post, I'm going to use PySimpleGUI to extract the sections of different newspapers online, using another library called newspaper
. To follow along after you have Python installed on your computer, open your terminal or PowerShell and enter the following command:
pip install pysimplegui
pip install newspaper3k
Now that you've installed the dependencies, you should be ready to follow along.
A Basic Application
Most PySimpleGUI application needs to include five sections:
- An import statement
- A layout
- A window creation statement
- An event loop
- A closing statement
To start creating the application, start by creating a Python file (e.g. app.py
). At the top of your file, begin by importing the two libraries that we installed earlier:
import PySimpleGUI as sg
import newspaper
To populate some of the dropdown menus that we will use in the layout, we're going to add one dictionary object that isn't standard:
publications = {'New York Times': 'https://www.nytimes.com',
'The Guardian': 'https://www.theguardian.com',
'Wall Street Journal': 'https://www.wsj.com'}
The layout for your application is created with a series of nested lists that we'll assign to a variable. The outer list represents the outer window of the interface, while each nested list is a row within the window. The items in each row are widgets that are part of the PySimpleGUI library.
layout = [[sg.Text('Pick a Publication: '), sg.DropDown(list(publications.keys()), key='selected')],
[sg.Text('', key='-OUT-')],
[sg.Submit('Select'), sg.Cancel()]]
With our layout created, we are now able to create the window object:
window = sg.Window("Pick Your Publication", layout=layout)
Finally, we'll create the event loop that will remain active until the window is closed or the Cancel button is pressed. This portion ends with the final recurring element in a PySimpleGUI app, the closing statement:
while True:
event, values = window.read()
if (event == 'Cancel') or (event == sg.WIN_CLOSED):
break
if event == 'Select':
publication = newspaper.build(publications[values['selected']])
window['-OUT-'].update('\n'.join(publication.category_urls()))
window.close()
To bring all of this together, I've written the application out as a whole below:
import PySimpleGUI as sg # the import statement
import newspaper
def main():
"""The keys in the dictionary below provide the items in the dropdown menu,
while their URL values are used to select which site newspaper's sections will
be listed in the display"""
publications = {'New York Times': 'https://www.nytimes.com',
'The Guardian': 'https://www.theguardian.com',
'Wall Street Journal': 'https://www.wsj.com'}
# the layout - each nested list below represents a row
layout = [[sg.Text('Pick a Publication: '), sg.DropDown(list(publications.keys()), key='selected')],
[sg.Text('', key='-OUT-')],
[sg.Submit('Select'), sg.Cancel()]]
# The window creation statement
window = sg.Window("Pick Your Publication", layout=layout)
# The event loop
while True:
event, values = window.read()
if (event == 'Cancel') or (event == sg.WIN_CLOSED):
break
if event == 'Select':
publication = newspaper.build(publications[values['selected']])
window['-OUT-'].update('\n'.join(publication.category_urls()))
window.close()
if __name__ == '__main__':
main()
With this relatively short script, I've been able to make a GUI that will allow users to see all the different sections of three major newspapers. While this only shows a small portion of what you can do with PySimpleGUI, I hope you look at some of the other examples that are available on its website: pysimplegui.readthedocs.io/en/latest
Once you've had a chance to try out this wonderful GUI library, I hope you will consider supporting it by either becoming a sponsor or making a one-time donation.