PyQT Application Structure




While we can code a simple window line-by-line without using Object Oriented Programming (OOP), we're going to find it to be quite the challenge to continue to ignore it as we improve the functionality of our application.

This tutorial is goign to be focused around laying the foundation for the growth of our application, using OOP. If you are not familiar with OOP, you can check out the Object Oriented Programming crash course tutorial, or try to follow along here. OOP can get quite in depth, but you only need to know the basics to make a lot of use out of it.

The fundamentals remain the same, we need an app definition, a GUI definition, and we need to always do a .show() somewhere to bring the window to the user. This is going to almost always look something like:


app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
Define the App.
Define the GUI (.show() will hide in here).
Run some simple exit code to ensure clean exit.

In the above code, the only thing that doesn't exist yet is this "Window" class that we're using to create the object that we're calling GUI.


Let's create this Window class:

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        self.show()
Create class, inherit from QtGui.QMainWindow

Create init method
Use super so we return parent object of this class
Starting points and size of window
Set title
Set window icon
Show the GUI

First off, the reason why we want to inherit from the QtGui.QMainWindow class is so that we can utilize all of the GUI aspects that QT gives us out of the gate, but we still want to wind up creating our own class for customization.

We use super so that this window returns the parent object, so that it acts like a QT object.

The rest of the code you have already seen, besides the icon, which should make sense.

Notice now that to reference various aspects, we use "self." Self references the current class, which, again, is a QT object, so this means with self. we can reference all of the methods that we've inherited from QtGui.QMainWindow, as well as any methods we write ourselves in this class.


...And we're done! As a reminder, the __init__ method is a method that will run open the creation of any object from the class. Whatever we put in here is basically the default of the application, then we can create new methods which we can allow to be called and interacted with, which will cause the window to change and do actions.

The full code up for this section is:

import sys
from PyQt4 import QtGui

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        self.show()

app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())

In the next tutorial, we will add a new "home" method, as well as add a button.


There exists 1 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • PyQT Basic Tutorial
  • PyQT Application Structure
  • PyQT buttons
  • Button Functions with PyQT
  • PyQT Menubar
  • PyQT Toolbar
  • Pop up Message PyQT
  • PyQT Check box
  • PyQT Progress bar example
  • PyQT Dropdown button and QT Styles
  • PyQT Font widget
  • PyQT Color picker widget
  • PyQT Text Editor
  • PyQT open files to edit
  • PyQT file saving