Wednesday, June 17, 2020

Real Python: PySimpleGUI: The Simple Way to Create a GUI With Python

Creating a simple graphical user interface (GUI) that works across multiple platforms can be complicated. But it doesn’t have to be that way. You can use Python and the PySimpleGUI package to create nice-looking user interfaces that you and your users will enjoy! PySimpleGUI is a new Python GUI library that has been gaining a lot of interest recently.

In this tutorial, you’ll learn how to:

  • Install the PySimpleGUI package
  • Create basic user interface elements with PySimpleGUI
  • Create applications, such as a PySimpleGUI image viewer
  • Integrate PySimpleGUI with Matplotlib
  • Use computer vision in PySimpleGUI
  • Package your PySimpleGUI application for Windows

Now it’s time to get started!

Free Bonus: Click here to get our free Python Cheat Sheet that shows you the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.

Getting Started With PySimpleGUI

PySimpleGUI was launched in 2018, so it’s a relatively new package compared with the likes of wxPython or PyQt.

PySimpleGUI has four ports:

  1. Tkinter
  2. PyQt
  3. wxPython
  4. Remi

PySimpleGUI wraps portions of each of these other packages and makes them easier to use. However, each of the ports has to be installed separately.

PySimpleGUI wraps the entirety of Tkinter, which comes with Python. PySimpleGUI has wrapped most of PySide2, but only a small portion of wxPython. When you install PySimpleGUI, you get the Tkinter variant by default. For more information about Tkinter, check out Python GUI Programming With Tkinter.

Depending on which variant of PySimpleGUI you use, applications that you create with PySimpleGUI may not look native to their platform. But don’t let this stop you from giving PySimpleGUI a try. PySimpleGUI is still quite powerful and can get most things done with a little work.

Installing PySimpleGUI

Installing PySimpleGUI is easy if you use pip. For the purposes of this tutorial, you’ll learn how to install the regular PySimpleGUI port, which is the Tkinter variant.

Here’s how to do it:

$ python -m pip install pysimplegui

This will install PySimpleGUI to whatever your system Python is set to. You can also install PySimpleGUI to a Python virtual environment. If you’re unfamiliar with Python virtual environments, then you should read Python Virtual Environments: A Primer.

If you prefer to try the PyQt variant, then you can use pip install PySimpleGUIQt instead. Now that you have PySimpleGUI installed, it’s time to find out how to use it!

Creating Basic UI Elements in PySimpleGUI

If you’ve ever used a GUI toolkit before, then you may have heard the term widgets. A widget is a generic term used to describe the elements that make up the user interface (UI), such as buttons, labels, windows, and more. In PySimpleGUI, widgets are referred to as elements, which you may sometimes see capitalized elsewhere as Elements.

One of the basic building blocks of PySimpleGUI is the Window(). To create a Window(), you can do the following:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()

Window() takes lots of different arguments—too many to be listed here. However, for this example you can give the Window() a title and a layout and set the margins, which is how big the UI window will be in pixels.

read() returns any events that are triggered in the Window() as a string as well as a values dictionary. You’ll learn more about these in later sections of this tutorial.

When you run this code, you should see something like this:

Read the full article at https://realpython.com/pysimplegui-python/ »


[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...