Getting started with PyQt


This article is a “getting started” or “installation” guide for PyQt.

The various commands that you’re going to see in the article below are what I like to call “Key commands” for PyQt. Why are they so important? It’s because (most of) these commands must be present in every PyQt application, regardless of approach or method. Without these your GUI will fail in one way or the other.

Once you’re done learning these, you can move onto the main tutorial where we teach you about all the different PyQt widgets and how to use them.

We’ll be using PyQt5, the latest version of the PyQt library in Python.


Installing and Setting up a PyQt5 GUI

First up is actually downloading and installing the pyqt5 library. You can do this in a variety of ways depending on your OS and setup. If you’re using the command prompt in windows, use the pip install pyqt5 command in your Python directory.

Next is the imports. Everyone has their own style of importing, so expect to see some difference between different tutorials. For the purpose of our tutorials, we’ll be importing in the following way throughout our website.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

This is just a standard number of imports for a basic application. You will require many more for a proper program, depending on the number of features and size of your application.

def window():
    app = QApplication(sys.argv)
    win = QMainWindow()

Here’s the function that’s going to get our PyQt application started. app = QApplication(sys.argv) is a compulsory line that you have to add, sort of like an initializer. Calling the QMainWindow() function will actually create the window on which we’ll be displaying our various widgets, however it won’t display it (yet).

    win.setGeometry(400,400,300,300)
    win.setWindowTitle("CodersLegacy")

Next we’ll be customizing our window with the setGeometry() and setWindowTitle() methods. setWindowTitle() will give our Window a name that appears on the top left hand corner of the window.

The setGeometry() method take 4 inputs in the format shown below.

setGeometry( X-coordinate, Y-coordinate, width, height)

The X and Y coordinate define the origin point of the Window when it is displayed. Keep in mind that these two coordinates do not define the center of the window, rather they define the origin point of the top left hand corner of the window. Passing ( 0, 0 ) as coordinates would draw the window to the top left hand corner of your screen.

These are the last two lines we need to launch a simple PyQt window.

    win.show()
    sys.exit(app.exec_())

While calling QMainWindow() will create the Window, it will not actually display it. For this we need to call the show() method on the window that we created, which is win in this case.

Finally there is the last line. This is another compulsory line that you need to add to all your PyQt applications. It’s necessary to achieve a “clean exit” from your GUI application. It waits until you’ve pressed the “X” button on your window, and then shuts down the application properly.


Full Code

Here’s a complete version of the code shown above. Remember to actually call the function to execute the code we wrote in it.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

def window():
    app = QApplication(sys.argv)
    win = QMainWindow()
    win.setGeometry(400,400,500,300)
    win.setWindowTitle("CodersLegacy")

    #.....

    win.show()
    sys.exit(app.exec_())
    
window() 

The comment in the above code, marks the location where we will be writing the bulk of our code.

To learn about using widgets in PyQt, follow this link to the main article.

Usually a class based approach is best and it’s also the one I recommend, but for teaching purposes I’ve decided to teach without the use of classes to keeps things simple and easy. Once the main content is complete, I’ll release several class-based JavaFX articles so you can learn that approach as well.


This marks the end of the Installation and Getting started with PyQt article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ujjwal gupta

from PyQt.QtWidgets

import QApplication, QMainWindow
this will through an error
please change this to
from PyQt5.QtWidgets

import QApplication, QMainWindow