≡ Menu

Working with PyQt: an empty GUI window

The following code walks you through the steps to create an empty GUI window.

# basic_window.py
# Import necessary modules
import sys
from PyQt5.QtWidgets import QApplication, QWidget
class EmptyWindow(QWidget):
    '''
    Create a new class, EmptyWindow, which inherits from other PyQt modules, 
    in this case from the QWidget class. 
    Inheriting from QWidget means we have access to all the attributes 
    to create GUIs using PyQt, but we also can add our own variables and 
    methods in our new class.
    '''
    def __init__(self):
        super().__init__() 
        '''
        create default constructor for QWidget; super is used to access methods 
        from parent class
        '''
        self.initializeUI()
        
    def initializeUI(self):
        """
        Initialize the window and display its contents to the screen.
        """
        self.setGeometry(100, 100, 400, 300)
        self.setWindowTitle('Empty Window in PyQt')
        self.show()

# Run program
if __name__ == '__main__':
    app = QApplication(sys.argv) # Create instance of QApplication class
    window = EmptyWindow()       # Create instance of EmptyWindow class
    sys.exit(app.exec_())        # Start the event loop

Your initial window should look similar to the one in the following figure depending upon your operating system.

Empty window created with PyQt5

Walking through the code, we first start by importing the sys and PyQt5 modules that we need to create a window. We commonly use sys in order to pass command-line arguments to our applications and to close them.

The QtWidgets module provides a set of UI (User Interfaces) elements that can be used to create desktop-style GUIs. From the QtWidgets module, we import two classes, QApplication and QWidget. You only need to create a single instance of the QApplication class, which manages the application’s main event loop, flow, initialization, and finalization, as well as session management.
Take a quick look at

app = QApplication(sys.argv)

QApplication takes as an argument sys.argv. You can also pass in an empty list if you know that your program will not be taking any command-line arguments using

app = QApplication([])

Next we create a window object that inherits from the class we created, EmptyWindow. Our class actually inherits from QWidget, which is the base class for which all other user interface objects are derived.

We need to call the show() method on the window object to display it to the screen.
This is located inside the initializeUI() function in our EmptyWindow class. You will notice app.exec_() in the final line of the program. This function starts the event loop and will remain here until you quit the application. sys.exit() ensures a clean exit.

If all of this is a little confusing as to why we have to create an application before
we create the window, think of QApplication as the frame that contains our window.

The window, our GUI, is created using QWidget. Before we can create our GUI, we must create an instance of QApplication that we can place window in.

Modifying the Window

The preceding EmptyWindow class contains a function, initializeUI(), that creates the window based upon the parameters we specify.

setGeometry() defines the location of the window on your computer screen and
its dimensions, width and height. So the window we just started is located at x=100,
y=100 in the window and has width=400 and height=300. setWindowTitle() is used to change the title of our window.

{ 0 comments… add one }

Rispondi