CodingNic

GUI Development

PyQt Introduction

GUI Development 42 min read

PyQt Introduction

PyQt Introduction

Tkinter is great for learning and small apps.

Another powerful option for desktop GUI development is PyQt.

PyQt is used for more advanced and professional desktop applications.


What Is PyQt?

PyQt is a Python library that lets you build GUI applications using the Qt framework.

Qt is a popular toolkit used to create cross-platform applications.

With PyQt, you can build apps for:

  • Windows
  • macOS
  • Linux

Why Learn PyQt?

PyQt is popular because it offers:

  • modern-looking interfaces
  • many advanced widgets
  • menus and toolbars
  • tabs
  • dialogs
  • tables
  • better large-app structure
  • professional desktop features

PyQt vs Tkinter

Tkinter

  • built into Python
  • simple to start
  • great for beginners
  • good for small apps

PyQt

  • external library
  • more features
  • modern UI tools
  • better for advanced apps

Both are valuable.


Install PyQt

In terminal:

bash
pip install PyQt5

First PyQt Window

python
import sys
from PyQt5.QtWidgets import QApplication, QWidget

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle("My App")
window.resize(400, 300)
window.show()

sys.exit(app.exec_())

What This Code Does

QApplication

Controls the GUI application.

QWidget

Basic window widget.

show()

Displays the window.

app.exec_()

Starts the event loop.


Add a Label

python
from PyQt5.QtWidgets import QApplication, QWidget, QLabel

label = QLabel("Hello", window)
label.move(20, 20)

Add a Button

python
from PyQt5.QtWidgets import QPushButton

button = QPushButton("Click Me", window)
button.move(20, 60)

Button Click Event

python
def hello():
    label.setText("Clicked!")

button.clicked.connect(hello)

Full Example

python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle("Demo")
window.resize(300, 200)

label = QLabel("Ready", window)
label.move(20, 20)

def change_text():
    label.setText("Clicked!")

button = QPushButton("Press", window)
button.move(20, 60)
button.clicked.connect(change_text)

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

Common Beginner Errors

Forgot show()

Window does not appear.

Forgot app.exec_()

App closes immediately.

PyQt Not Installed

Install with pip.

Missing sys.argv

Use standard startup pattern.


When to Choose PyQt

Choose PyQt when you need:

  • bigger apps
  • richer UI
  • advanced widgets
  • professional desktop software

Code Along

Create a PyQt window with:

  • title = Student App
  • size = 400x300

Mini Challenge

Build a PyQt app with:

  • Label = 0
  • Button = Add

Each click increases the number.


Real World Use Case

PyQt is used for business tools, dashboards, editors, data apps, and professional desktop software.


Quiz

  1. What is PyQt?
  2. How is it different from Tkinter?
  3. What does show() do?
  4. How do you connect a button click?

Assignment

Create a PyQt app with a label and two buttons:

  • Green button changes text to Success
  • Red button changes text to Error

Summary

You learned the basics of PyQt and how to build a simple desktop GUI with labels, buttons, and events.