PyQt window

I'll show you a small PyQt window like the one shown below.

You can maximize, minimize, or terminate the size of a window with the built-in buttons in the upper-right (Windows) or upper-left (macOS) of the window. You can also move the window with the mouse or resize the window.

These features actually require a lot of code, but they're often used in most applications, so someone has already made them.

Related course: Create Desktop Apps with Python PyQt5

PyQt window

The example below creates a PyQt window, which you can drag, resize, maximize etc. The PyQt5 module needs to be installed.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('My First Application')
        self.move(300, 300)
        self.resize(400, 200)
        self.show()

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = MyApp()
   sys.exit(app.exec_())

These few lines of code float a small window on the screen.

pyqt window

Description

import sys
from PyQt5.QtWidgets import QApplication, QWidget

Load the modules you need. Widgets (classes) that provide basic UI components are included in the PyQt5.QtWidgets module. All classes included in the QtWidgets module and a detailed description of them can be found in the QtWidgets official document.

self.setWindowTitle('My First Application')
self.move(300, 300)
self.resize(400, 200)
self.show()

Where self is a MyApp object. * The setWindowTitle() method set the title of the window that appears in the title bar. * The move() method moves the widget to a position of x=300px and y=300px on the screen. * The resize() method resizes the widget to 400px wide and 200px high. * The show() method shows the widget on the screen.

if __name__ == '__main__':

'name' is a built-in variable in which the name of the current module is stored. If you import .py code called 'moduleA', the name code will be 'moduleA'. Otherwise, if you run the code name, main is the same. Therefore, this single line of code ensures that the program runs directly or through modules.

app = QApplication(sys.argv)

All PyQt5 applications must create application objects.

Related course: Create Desktop Apps with Python PyQt5