Python[Eng]/PyQt[Eng]

[Python] Creating a Simple GUI with PyQt5 and PyQt5-Tools

CODE.J 2025. 3. 1. 21:55
반응형

Using PyQt5 for GUI Development in Python

Python provides a built-in GUI module called tkinter. However, since creating a GUI with tkinter requires writing scripts manually, I found it somewhat inconvenient and restrictive.

So, I started looking for alternatives and found PyQt5 and PyQt5-Tools, which allow me to design a GUI easily and then add functionality to it. (Productivity boost!)

Installing PyQt5

Run the following commands in the command prompt to install the required packages:

pip install PyQt5  
pip install pyqt5-tools
 

Once the installation is complete, you can launch the GUI design tool by running:

{Python installation path}\Lib\site-packages\qt5_applications\Qt\bin\designer.exe

This tool lets you create and arrange GUI components visually.


Creating a Simple GUI with PyQt5

Let's start by creating a basic GUI using a Widget. Since our goal is just to display a window, we will save the design without adding any functionality for now.

 

💡 Tip: You can press Ctrl+R in the designer tool to preview your GUI before implementing it in Python.

 

After designing the GUI, save the project in your desired folder. For this example, I'll name my Python script "pyqt_test.py".

Writing the Python Script

Create a new Python file and write the following code:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic

form_class = uic.loadUiType("untitled.ui")[0]

class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()
 
Running the Code

When you run this script, you should see a simple window pop up.

From this basic setup, we can start adding functionality by linking objectNames from the UI to functions in the MyWindow class.

I'll cover how to do that in the next post! (Still deciding what to build... 🤔)

반응형