Python GUI with PyQT/PySide2

A look and guide at a popular GUI for your projects.

Keno Leon
Keno Leon
Mar 7, 2020 · 11 min read

I’ve been searching for a GUI for one of my Python projects for a while now, at some point I thought Kivy was the one, I still think it is a really cool and useful GUI ( especially for prototyping and mobile app development), and I’ve tried others like tkinter which I can recommend for smaller projects, QT,PyQT and PySide2 ( which are all related) consistently have been recommended, but this recommendations also came with warnings, plus it seems a bit intimidating from the outset, here we will check it out, some code for starters…

# Hello World:

PySide is complex and verbose, mastering it in a few sessions realistically might not happen but we can at least figure out some of the basics.

Main Docs :

Widgets

PySide has multiple levels of abstraction and a variety of modules, I think a good place to start with PyQt is via widgets which are components that you can drop in your app, note that you can also extend and make your own widgets if the control you want is not included but the out of the box list covers a lot:

Interacting with widgets via slots & Signals

The meat and potatoes of GUI’s usually consists in getting user’s input data out of the GUI into your script and also sending data changes to the GUI for display, PyQt does this through slots and signals:

Getting a sliders value :

Layout

Another thing one does ( or usually struggles ) when dealing with GUIs is laying out elements across the page or app, PyQt has straightforward layouts out of the box:

Ascii Count Von count says..

You can certainly make more complex layouts but before you do ( by nesting or creating custom widgets ) check the available ones:

You might also want to explore the mainWindow Widget/class that provides a framework for common placement of widgets:

Theming & Styles:

Let’s be honest, what we really want as developers in a GUI is dark mode and PyQt provides a lot of flexibility in colors and font via themes, creating a dark mode is simplish:

More on themes, styles and custom widgets, note that you can load a custom CSS stylesheet for widgets and/or change palettes or create a widget’s appearance from scratch…

Integrations

Another common thing one does with GUis is integrating them with other libraries or parts of your script, usually by gently hammering them in place which usually goes as smooth as you’d expect, writing your app around a GUI or having conflicting libraries is common, so with that said, let’s check a couple of PyQT/PySide integrations…

PyQt/PySide Modules:

Before you go the external route, PyQT/PySide gives you several modules that might allow you to skip integration all together, let’s say you want a chart, instead of using a chart library you could use PyQt/PySide’s own chart module:

I’d recommend this route without hesitation except for the previous caveat, so you might want to check the existing modules and spend some time getting a feel for the existing documentation and workflow…

External libraries

By external libraries I mean some hardware IO or graphical element created outside of QT and the question is how hard is it to integrate them with QT, let’s start with a common chart library like matplot/pyplot

Pyplot turned out to be really easy since there is support for PyQt right out of the box, let’s check out another common case, graphics using the QPixmap widget :

A cool VW van I saw while walking around.

Also pretty easy, in general you want to use a container which could be a built-in widget or make you own widget which we won’t fully cover here (see next example though) and its where I’d point you next if you need more complex library integrations ( Qt’s tutorials cover most of what you will need ):

Hardware Integrations

And lastly there is the case where you need to interact with the real world via your GUI, these are somehow complex cases in that there is a lot of data being interchanged and sometimes a process collides with each other in time and your app crashes.

As luck would have it, this is precisely what my personal project needs (without the crashing), specifically I need a GUI that interacts with my webcam and OpenCv :

The exciting view from my coffeeshop table.

Leftovers

I’ve tried to make this a practical overview but there is much more to PyQt that I could fit in here, some of the things I haven’t tried yet that caught my eye:

QT Designer A QT app for designing GUIs, Gestures, Web capabilities there’s a built in web browser and other bits, 3D Support, Database integrations, Creating custom widgets, unit tests, painting and drawing custom shapes in a canvas, widget stylesheets.

Conclusions

In my experience QT/PySide/PyQT…is powerful, it’s also complex and poorly documented, a fractured ecosystem and a source of a lot of confusion while getting started, yet the end results if you plow through are beautiful robust and functional GUIs in python, well worth the time if you have it and I hope this post helps you get there.

Thanks for reading.

The Startup

Get smarter at building your thing. Join The Startup’s +793K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Keno Leon

Written by

Keno Leon

AI, Software Developer, Designer : www.k3no.com

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Keno Leon

Written by

Keno Leon

AI, Software Developer, Designer : www.k3no.com

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store