First Steps with Python + Qt # 1

This tutorial is focused on teaching the use of the QT framework using Python as a programming language, for this we can use both PySide and PyQt, but in this tutorial we will use PySide because the author of this tutorial likes the LGPL PySide license better. than the GPL or PyQt Commercial.

First ... What is Python?

According to Wikipedia:

"Python is a very high-level interpreted programming language whose philosophy emphasizes very clean syntax and favors readable code."

Python is a multipadradigm programming language, so it supports different python programming paradigms, for example it supports: Object Oriented Programming (OOP), Imperative Programming (Structured) and to a lesser extent Functional programming.

Only that?

No, it is also a dynamic typing language, which means that python allows us to assign different types of values ​​to the same variable during the execution of the program, some see it as an advantage, others as a disadvantage, see for yourself.

In addition to what has already been mentioned, Python allows us (like many others), programming both in the web environment and in the web desktop, it is also cross-platform, currently it supports Gnu / Linux, Windows, Mac osx, Android (If you need to mention them for your modification).

What is QT?

QT is a multiplatform framework, originally developed by Nokia, to later be sold to Digia, which is used for application development, it is written in C ++, but can be used in other languages ​​through Bindings (in our case PySide or PyQt )

Differences between PySide and PyQt

Mainly

  • License, PySide is licensed under the terms of the LGPL and PyQT is under the terms of the GPL or under a commercial license, so we would need to pay in case of creating a closed application.
  • PySide is created by Nokia, and PyQt by RiverBank.
  • PySide saves us the trouble of using STR every time we use the QString of each of the QT objects.

We can detail the complete changes Here.

Preparation:

We install Qt:

If we have a distro with Kde we should already have what we need, if not:

sudo apt-get install qt4-dev-tools

We install PySide:

sudo apt-get install pyside pyside-tools

Our First Python + QT Application

We go to our favorite text editor, I recommend that you install sublime-text2, but you can use the one you prefer, and we type the following code and save it with the name that we want always ending with the extension ".py", I will I'll save as app1.py:

3

See Code

We run it from the console like this:

python app1.py

This window should appear

2

What? Only That, because if you only had a console before, what are you complaining about? What did you expect, an Erp? Come on man, something starts.

Now let's explain the code:

from PySide.QtGui import *

from sys import exit, argv

Here we import the necessary modules for PySide execution.

app = QApplication(argv)

We create the Qapplication instance with the name app.

window = Qwidget()

We create the QWidget () instance.

window.setWindowTitle("Primera App PySide")

We execute the setWindowTitle method, which receives a String as a parameter to "set" the title of our Qwidget.

window.show()

We show our window

exit(app.exec_())

finally we execute the infinite cycle of execution of our QApplication ().

Thank you

Thank you for those who have followed me here and have been useful. For the next tutorial I will explain how to design our windows with QT Designer and import them into our projects.

Additionally, I add the link to the forum desde linux where I have a small theme with a small library of python tutorials and guides, containing different terms including:

Basic Python Tutorials

wxPython

PyQt and PySide

Django

web2py

Scientific programming in python.

And many more.

LINK TO THE FORUM

I hope they serve you, I say goodbye, as always a pleasure to belong to this wonderful community.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   @ trust17 said

    Thanks for the post!

  2.   anubis said

    2 things:

    1. Qt was not originally developed by Nokia, but by Trolltech, which was later bought by Nokia. The latter then sold Qt to Digia.
    2. The image in the first application example does not match the code.

    Thanks for the tutorial, I'll follow it 🙂

    1.    f3niX said

      You're absolutely right I missed it. And the second thing you are also right, is that I changed it to make it easier and not use class and I missed the modification, beginner errors. I already pass the corrections for some editor to approve them.

    2.    f3niX said

      I can not modify, I leave it to an editor.

      Greetings.

      1.    neysonv said

        Email an editor with the corrected tutorial. You can send it to the email of @nano who is one of the editors
        mailto: nano.world.contact@gmail.com
        please don't leave the tutorial like this
        regards

  3.   Germán said

    One thing strikes me: why do you use "from import *" in your code?

    Regards!

    1.    f3niX said

      It is the correct way to import, when importing like this you tell it which specific class of the module you want to import, and you do not call the complete module.

      Greetings.

      1.    Germán said

        I find that kind of confusing. Furthermore, it is not recommended in PEP8:

        "Wildcard imports (from module import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools ..."

        1.    f3niX said

          I have been guided mostly by professional codes, such as Ninja-Ide, you can see the code here: https://github.com/ninja-ide/ninja-ide/blob/master/ninja_ide/core/ipc.pyIt is done in PyQt as well, but in any way there is nothing that forces you to use one form or the other.

          Here you can see a discussion on StackOverflow, about this:

          http://stackoverflow.com/questions/710551/import-module-or-from-module-import

          There they mention pros and cons of each of the styles.

          1.    f3niX said

            ok sorry I did not understand you, I know that, I only did it because I was not going to give an explanation of each element QApplication, QWidget, then generalize but you are absolutely right.

            Excuse the misunderstanding.

          2.    Germán said

            No, you didn't understand or you didn't pay attention ... I mean using «from module import *», that is, I mean the «asterisk» ... in that StackOverflow post they are talking about something else, but they still emphasize it:

            "Either method is acceptable, but don't use from module import *."

            Is it understood now?

          3.    Felipe said

            German is right. What use is the from module import if you import all the content with the asteric, for that import module. Slds just as nice post.

          4.    Germán said

            Ahh well, now I understand… anyway, don't take me too long, I'm also a beginner in Python. If you stop by my blog, you will see that I made a post similar to yours but with Gtk + 3 using PyGObject.

            Greetings and thanks!

        2.    f3niX said

          Here I leave the Pep8, in Spanish recommending the use of im
          It is highly inadvisable to use relative imports to import code from a package. Always use the absolute package path for all imports. Even now that PEP 328 [7] is fully implemented in Python 2.5, using relative imports is seriously discouraged; absolute imports are more portable and usually more readable.

          When importing a class from a module, it is usually correct to do this
          from myclass import MyClass
          from foo.bar.yourclass import YourClass

          regards

          1.    dhunter said

            Read this: http://stackoverflow.com/questions/3615125/should-wildcard-import-be-avoided

            Alex Martelli (core py developer) recommends something like this:


            from PyQt4 import QtCore, QtGui

  4.   msx said

    I was wanting more, for when the 2nd part !?
    regards

  5.   eliotime3000 said

    Very good tutorials, plus it might be more comfortable to program.

  6.   dhunter said

    Here is an example of an interface from scratch (without using QtDesigner).

    https://github.com/xr09/metrocontador

    1.    f3niX said

      The dhunter app is excellent, here we don't even know how much they charge us for electricity, haha ​​greetings.

      1.    dhunter said

        Thank you, it is one of those apps that one makes for personal use and serves others. Here in Cuba the receipt is itemized with the rate, it is easy to copy the algorithm.

  7.   Jose Eloy said

    I will be very aware of this course. I've been interested in making cross-platform applications for a long time and I think the Python + QT / Pyside combination is a great option. Greetings and congratulations for the initiative

  8.   Bryan Rodriguez said

    I followed all the steps exactly as you say but when I run the app1.py application in the terminal it tells me Traceback (most recent call last):
    File "app1.py", line 1, in
    from PySide.QtGui import *
    ImportError: No module named PySide.QtGui. For what is this?.

    1.    Felipe said

      that's because you are using a version of python as the default interpreter, but you don't have pyside installed for that version.

      Try adding at the beginning of the file:
      #! usr / bin / python3
      or else
      #! usr / bin / python2

      I recommend you use Eclipse with pydev and configure the interpreter correctly.
      slds

      1.    Felipe said

        #! / usr / bin / python3
        or else
        #! / usr / bin / python2

        1.    f3niX said

          PySide is not yet compatible with Python3, be careful with that, and secondly, it checks the imports through the interactive python console, so you know you have it installed or not, it also installs pip.

          1.    Bryan Rodriguez said

            How do I verify it?

          2.    f3niX said

            In a console use "Python", anyway I recommend you enter the library that I left below in the post and download "python for everyone", you need more base.

            Greetings and luck.

          3.    Bryan Rodriguez said

            Thank you very much, it was very helpful to me.

          4.    Mauricio Tobares said

            look friend F3niX I found this, it seems that for python 3.3.2 on windows (which is what I have until next week) they have binaries for 32 bits and 64 bits, I tried your example and it works with python 3.3.2 and I use The PyScripter that installs different environments, if you open the python 3 it runs the scripts as python 3 and if you open the 2.7 it executes them as 2.7 and so on (logically you have to have that version of python installed ... it is understood)

            http://qt-project.org/wiki/PySide_Binaries_Windows

            I'll leave you there, gossip, maybe it will serve someone else 😀

      2.    Bryan Rodriguez said

        I get the same error.

        1.    Germán said

          Do you have PySide installed?

          1.    Bryan Rodriguez said

            I have it installed.

        2.    Felipe said

          And try with PyQT4 install it and then change the PySide lines for PyQT4 or PyQt4, and give it run. jojo. Use the eclipse. Luck.

          1.    Bryan Rodriguez said

            How do I do that?.

  9.   karlinux said

    Thank you very much !!! Waiting for the second chapter

    1.    f3niX said

      For this Friday I may publish it (The university has me a little full) greetings.

  10.   MauricioTobares said

    Friend F3nIX I'm waiting for your tutorial N ° 2, 3, 4… N hehehe

    I already have my very elegant linux installed with the PySide waiting and everything!

    Let's see when he is encouraged to show a new tutor 😀

    1.    f3niX said

      Excuse me friend, I have it in drafts but work and studies have not given me time, I promise to get something out this week.

      A big greeting

  11.   Dante Alighieri said

    Hello, I have been interested in learning Python for a long time but I have some doubts. What I want is to make an application to improve a little the information process of the family business where I work, I would like to do something for windows, with windows at first, it would work all on a single pc and then that it can be connected via internet to the database to which a couple of computers would connect. There is a lot of diversity in terms of GUIing the application and that makes me dizzy. What do you recommend to give it a modern and attractive look? I would also do some statistical graphics and print reports, what can I use for that? Cheers

  12.   George said

    Hello, very good your post, I have a question, could I sell my app made with pyside without any problem?

    1.    f3niX said

      Well, licenses are always crazy, but free software licenses also allow you to sell software, what happens is that the GPL forces you to distribute the code, unlike the LGPL, which does not force you to do so.

      Anyway, I'm not an expert on licensing.

      Greetings.