67

I am looking for a PyQt5 tutorial. It is rather complicated to start GUI development with Python for the first time without a tutorial.

I only found some PyQt4 tutorials so far, and since something changed from Qt4 to Qt5, for example the fact SIGNAL and SLOT are no more supported in Qt5, it would be nice to have specific tutorials for PyQt5.

Can someone please provide a tutorial on how to start GUI development with PyQt5?

nbro
  • 15,395
  • 32
  • 113
  • 196
WeGi
  • 1,908
  • 1
  • 21
  • 33
  • 2
    is this ok: http://pyqt.sourceforge.net/Docs/PyQt5/ – zhangxaochen Jan 08 '14 at 13:08
  • 1
    Its a start, and i allready tried looking there, but its really more of a Documentation than a tutorial. – WeGi Jan 08 '14 at 13:12
  • 3
    @WeGi. You should read [Differences Between PyQt4 and PyQt5](http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html). Once you've understood that, you can easily translate PyQt4 tutorials to PyQt5. In fact, this would be a good learning exercise in itself. Note that it's still useful to know the [old-style signal and slot syntax](http://pyqt.sourceforge.net/Docs/PyQt4/old_style_signals_slots.html), because that is what you will find in the [Qt docs](https://qt-project.org/doc/qt-5/index.html). – ekhumoro Jan 08 '14 at 18:35
  • @WeGi I may be late to answer this, but you may be interested in this PDF I found, while also trying to learn PyQt5 – Inversus Jul 10 '14 at 09:48
  • @WeGi If you're still interested in learning PyQt5, I've been on the same quest and had the same issues (old PyQt, etc). I have found a better tutorial on MVC for PyQt that is actually PyQt5. Check it out, if you're still looking into this stuff. I'll update my answer, as time goes on – Inversus Jul 21 '14 at 01:05
  • @Inversus Accpeted your answer, many thanks. – WeGi Jul 21 '14 at 08:36
  • @WeGi Many thanks to you, too, my friend. As promised, I'm here with some more goodies (more to come, too). Enjoy! – Inversus Jul 31 '14 at 22:47
  • Since the question was closed as off-topic, I've edited its wording so that it's clear that it's asking for a tutorial (which I provided/am providing in my accepted answer). I'm pretty sure that was the asker's intention, and should satisfy the on-topic requirement (as it's now a direct question with an answer and can no longer be misinterpreted as a request for tool recommendations). I would like to request that this question please be reopened. Thanks :) – Inversus Sep 05 '14 at 12:44
  • @Inversus - I think it was originally interpreted as a request for *tutorial recommendations*, which would most likely be off-site, and therefore off-topic, but since you have directly answered the question, it might be ok. Perhaps the word "tutorial" should be edited out of the question (to me, it suggests an article on another site). – Justin Sep 05 '14 at 12:58
  • 6
    @Justin I was previously instructed to make it clear that this is asking for a tutorial, so it would seem to be a step backwards to edit that out now. Additionally, this is the most relevant link on Google for a good high-level tutorial and links to resources for people new to PyQt5. Another boon for StackOverflow that could have been so much shinier, had the tutorial's author not gotten discouraged when the question was closed over overly strict technicalities (or perhaps edit-badge mining).. I would ask again, humbly, for you to please re-open this question. Thank you for your consideration. – Inversus Nov 04 '14 at 15:08
  • I would be far more encouraged to expand on my answer if the question were not mistakenly closed as off-topic. :) – Inversus Nov 04 '14 at 15:10
  • A Google search of something like `pyqt5 tutorial` provides some examples, particularly for beginners. I found one of these useful to understand how to get started and the basics. – Edward Oct 03 '16 at 19:31
  • Here is a YouTube tutorial specifically for that purpose. Seems to be the simplest method out there. https://www.youtube.com/watch?v=mBvpoNLb654&t=1s – wattahay Apr 17 '18 at 00:57
  • Buy this e-book: https://amzn.to/2Se08MU – Boštjan Mejak Jul 13 '19 at 16:38
  • A series of PyQt5 tutorials: https://youtu.be/ORaBSFhh13A – Trees Sep 24 '20 at 10:00

3 Answers3

78

As my travels into the depths of PyQt5 continue, so shall I continue to update this answer with some of the shinier treasures I find.

That being said, I am now taking a "rough draft" stab at a quick intro to PyQt5. I will also provide links to helpful resources. I am new to this framework as well, and I will elaborate on what I believe to be a good strategy for using it, as I figure that strategy out. There are likely other good strategies, so if anyone has anything to add, then please leave a comment. This is very much a work in progress.


Strategy

I've learned much from the example code as suggested in the other answer, but something the examples don't help with is PyQt5's deep magic. Frameworks with a lot of magic in them (PyQt5, Django, SQLAlchemy, ...) are great because an enormous amount of drudgery is abstracted away from you. On the flip side, it is not always clear what the hell is going on, or what you're supposed to do about it.

Luckily, it seems we have options:

  • QtDesigner: For those days when your keyboard catches fire, there's a rockin' GUI-Builder called in the installation package. When you see the code this produces (perhaps only in the community version?), you'll see why this may not be the panacea it seems.

  • QML: Another candidate for panacea: declarative GUI building from formatted JSON. Yum.

  • Qt Quick: The framework for QML. By this point, it may seem tantalizingly easy, but don't get sucked in by this stuff just yet. It always seems to come down to learning it by hand.

  • The Model-View Framework(1): Model-View (not MVC) separates the code that deals with presentation/interaction from the code that manages the data, with the aim of providing modularity.

Coding in PyQt5 is greatly simplified by using the set of classes that implement the Model-View design pattern. Model-View is an evolution of Model-View-Controller (MVC), in which the Controller has been reunited with the View. They seem like strange bedfellows, but, most of the program's logic is dealing with either the user, or data: it seems to make a certain sense, at least at a stratospheric level.

From a bird's eye:

Architecture(s)

Model-View-Controller

This widely-used design pattern separates the application into 3 layers:

  1. Model ~> Encapsulates the data. Notifies View and Controller of any changes to the underlying data. This causes updates to the display of output or available commands, respectively.
  2. View ~> Displays the relevant output from the Model to the user.
  3. Controller ~> Encapsulates user interaction, and notifies the Model and View of relevant events.

Model-View

  • The Graphics View Framework(1) ~> Represent everything (including embedded QWidgets, etc) inside a QGraphicsScene as a QGraphicsItem (or derivative thereof), including proxy classes for embedding widgets. The items are supposedly highly optimized, and integrating OpenGL support is a one-liner, which is nice.

This design pattern puts the Controller inside the View. This way, the view is capable of handling the entirety of the user's interaction. In concrete terms, these are the Signals and Slots mechanisms.

User Interaction Management

Callbacks

Signals and Slots

..... ** I'm sorry, but I must sign off now. I'll be back to continue to add to this. **

Practical Example(s)

Like, for instance, you can take a tree view from the itemviews/editabletreemodel example, then swap in a file system model (QFileSystemModel) from the itemviews/dirview example and you've got a full (working) view of your directory tree. Pretty snazzy.

So, you would take the code from the editabletreemodel example:

headers = ("Title", "Description")

file = QFile(':/default.txt')
file.open(QIODevice.ReadOnly)
model = TreeModel(headers, file.readAll())
file.close()

self.view.setModel(model)

...and swap in the model from dirview:

model = QFileSystemModel()
model.setRootPath('')
self.view.setModel(model)

...and it just works. Amazing.

The next step (in my case) (*I think) is implementing a custom model which I will then use several views concurrently, but I don't know if that kinda thing fits your use case.

Resources

Here are some gems I found on my travels. Hopefully they help you on yours.

This is a tutorial on Model-View for Qt5.(1) It is a very detailed document from the official Qt5 docs. A good deal of useful documentation can be found at the Qt5 site. Keep in mind, it's for Qt5 (the C++ library), but the difference is trivial to read through (and the PyQt5 official docs point there anyway).

This PDF contains a quick high-level to PyQt4's Model-View framework. Note that is it for PyQt4 (not PyQt5), but it is actually for Python (as opposed to C++), and I found it very quickly taught me a lot.

I am just starting to play with the Graphics View, and am finding this tutorial on the Graphics View Framework very helpful. This is the same View that is used in the qtdemo example code to generate some slick effects. I'll be updating this in a bit.

This is a complete list of all of the Qt5 Modules.

This is a complete list of all of the Qt5 Classes.

This is a complete list of all functions in the Qt5 API.

As katsh pointed out in another answer's comments, here is a link to the example code for PyQt5.2.1 on GitHub

Additionally, a copy of the example code comes packaged with your distribution and can be found at:

%PYTHON_HOME%\Lib\site-packages\PyQt5\examples

If you're using PyDev (Eclipse), you can run examples by simply right-clicking an example's main module file in PyDev Package Explorer or Navigator =:> Run As =:> Python Run

The best one, in my (not so) humble opinion, is:

%PYTHON_HOME%\Lib\site-packages\PyQt5\examples\qtdemo\qtdemo.py

Among my current projects, I'm in the process of reverse engineering this example. If you check it out, you'll see why. To be continued.. ;)

Enjoy!

nbro
  • 15,395
  • 32
  • 113
  • 196
Inversus
  • 3,125
  • 4
  • 32
  • 37
  • I'll soon rework my entire answer after learning a bit more about PyQt5. So please don't downvote. I know it's messy and I'll clean it up :P – Inversus Jul 21 '14 at 01:17
  • 2
    Completed a good chunk of a massive rewrite. It's still not finished, and I'll be back to continue later... (and I'll delete these comments when I do) – Inversus Aug 01 '14 at 00:23
  • 2
    It's been 2 years. I was wondering if you were interested in updating this answer. I could help but I don't know much about PyQt5 and every search I do just leads me back to here eventually. – naiveai Nov 25 '17 at 15:20
29

Been looking for PyQt5 tutorials for some time? Look no further! You won't find many around the internet.

Not really tutorials, but pretty self-explanatory basic scripts under the following path:

/python/lib/site-packages/PyQt5/examples

you will find about 100 examples in 30 folders ranging from beginner to advanced, covering basic windows, menus, tabs, layouts, network, OpenGL, etc.

nbro
  • 15,395
  • 32
  • 113
  • 196
Pete
  • 291
  • 2
  • 2
  • 9
    Can also be found here https://github.com/baoboa/pyqt5/tree/master/examples – sqram Aug 08 '14 at 04:00
  • 1
    @katsh Thanks. I'll update my answer with your link. I had forgotten to include that in my answer, thanks for (indirectly) reminding me. – Inversus Aug 08 '14 at 16:47
  • I wasn't knocking your answer. Pointing people to the examples was the right thing to do. – RobotHumans Aug 08 '14 at 21:21
  • @hbdgaf I know you weren't. I'm not finding fault with that at all. It is the right thing to do. I was just leaving a note for any passersby pointing them to the times, when they could see themselves. Still, it was tough to say while trying not to put words into your mouth or insult a good answer. Thanks for the teardown, though. Take care! :) – Inversus Aug 08 '14 at 23:19
  • 2
    `pip3` on MacOS X doesn't install examples. `/usr/local/lib/python3.6/site-packages/PyQt5/examples` is not there. – Anton Tarasenko Oct 29 '17 at 08:20
8

Have a look at http://www.thehackeruniversity.com/2014/01/23/pyqt5-beginner-tutorial/ This is a newbie friendly tutorial

user1801060
  • 2,733
  • 6
  • 25
  • 44
  • 1
    That's not a very good resource. The only example of that page isn't indented correctly, so it won't work. – mata Jan 24 '14 at 07:51
  • Works for me, maybe they fixed it. – dmolony Feb 11 '14 at 23:06
  • Worked for me too. But I really want to know how to use Qt Designer with PyQt5 --- couldn't find any step-by-step examples, i.e, which kind of project template are you supposed to select..... – David Sep 14 '14 at 14:51