Create a Basic Window in PyQt6

When I first started learning about GUI development in Python, PyQt6 immediately stood out as one of the most powerful frameworks available. Today, I will walk you through creating your first window in PyQt6, a fundamental skill that will help as the foundation for building more complex desktop applications.

Prerequisites

Before we get in, make sure you have:

  • Python 3.6 or newer installed
  • Basic knowledge of Python programming
  • A text editor or IDE (I recommend PyCharm or VS Code)

Read QComboBox Widget in PyQt6

Install PyQt6

First, let’s install PyQt6 using pip:

pip install PyQt6

Create Your First PyQt6 Window

Let’s start with the most basic PyQt6 application – a simple window. Here’s how you can create one:

import sys
from PyQt6.QtWidgets import QApplication, QWidget

# Create the application object
app = QApplication(sys.argv)

# Create a window object
window = QWidget()
window.setWindowTitle("My First PyQt6 App")
window.setGeometry(100, 100, 400, 300)  # (x, y, width, height)

# Show the window
window.show()

# Start the event loop
sys.exit(app.exec())

This code does a few key things:

  1. Imports necessary modules
  2. Creates a QApplication instance
  3. Creates a QWidget (our window)
  4. Sets window properties
  5. Shows the window
  6. Starts the event loop

When you run this code, you’ll see a simple window appear on your desktop with the title “My First PyQt6 App”.

You can see the output in the screenshot below.

Window in PyQt6

Check out Create a Random Number Generator with QLCDNumber in PyQt6

Different Methods to Create Windows in PyQt6

PyQt6 offers multiple approaches to creating windows. Let’s explore the three most common methods:

Method 1: Use QWidget (Basic Window)

We’ve already seen the QWidget approach. This creates the most basic window possible and is perfect for simple applications.

Method 2: Use QMainWindow (Full-Featured Window)

For more complex applications, QMainWindow provides additional features like menus, toolbars, and status bars:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QMainWindow Example")
        self.setGeometry(100, 100, 500, 400)
        # You can add menus, toolbars, etc. here

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

You can see the output in the screenshot below.

Basic Window in PyQt6

Read Build a Simple Digital Clock with QLCDNumber in PyQt6

Method 3: Use Object-Oriented Approach

For larger applications, it’s best to use an object-oriented approach by creating custom window classes:

import sys
from PyQt6.QtWidgets import QApplication, QWidget

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle("OOP Window")
        self.setGeometry(100, 100, 300, 250)
        # Add widgets and layouts here

app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())

You can see the output in the screenshot below.

Create a Basic Window in PyQt6

Check out QSpinBox Widget in PyQt6

Customize Your Window

Now that we know how to create basic windows, let’s explore how to customize them.

Set Window Size and Position

# Format: setGeometry(x, y, width, height)
window.setGeometry(100, 100, 400, 300)

# Alternatively, you can set size and position separately
window.resize(400, 300)  # width, height
window.move(100, 100)    # x, y

Set Window Title and Icon

from PyQt6.QtGui import QIcon

window.setWindowTitle("Custom Title")
window.setWindowIcon(QIcon('path/to/icon.png'))

Fixed vs. Resizable Windows

By default, PyQt6 windows are resizable. To create a fixed-size window:

window.setFixedSize(400, 300)

Window Styles and Appearance

PyQt6 offers several window flags that can change how your window appears and behaves:

from PyQt6.QtCore import Qt

# Create a window with no title bar
window.setWindowFlags(Qt.WindowType.FramelessWindowHint)

# Create a window that stays on top
window.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)

# Create a tool window (with smaller title bar)
window.setWindowFlags(Qt.WindowType.Tool)

Read QCheckBox Widget in PyQt6

Common Window Properties

Here’s a table of common window properties you might want to set:

PropertyMethodDescription
TitlesetWindowTitle()Sets the window title
Sizeresize()Sets the window size
Positionmove()Sets the window position
Minimum SizesetMinimumSize()Sets the minimum allowed window size
Maximum SizesetMaximumSize()Sets the maximum allowed window size
Fixed SizesetFixedSize()Makes the window non-resizable
Window IconsetWindowIcon()Sets the window icon
Window OpacitysetWindowOpacity()Sets the window transparency (0.0-1.0)

Create Multiple Windows in PyQt6

In more complex applications, you might need multiple windows. Here’s how to create and manage them:

import sys
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
)

class SecondWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Second Window")
        self.setGeometry(150, 150, 300, 200)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")
        self.setGeometry(100, 100, 400, 300)
        
        # Create a central widget and layout
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout(central_widget)
        
        # Add a button to open the second window
        self.button = QPushButton("Open Second Window")
        self.button.clicked.connect(self.open_second_window)
        layout.addWidget(self.button)
        
        # Create but don't show the second window yet
        self.second_window = SecondWindow()
        
    def open_second_window(self):
        self.second_window.show()

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

Check out QRadioButton Widget in PyQt6

Best Practices for PyQt6 Window Design

After creating hundreds of PyQt applications, I’ve found these best practices to be essential:

  1. Use object-oriented programming – Create custom window classes for better organization
  2. Separate UI and logic – Keep your UI code separate from your business logic
  3. Use layouts – Always use layouts instead of absolute positioning for responsive UIs
  4. Consider screen resolution – Test your app on different screen sizes and resolutions
  5. Follow platform guidelines – Your app should respect the conventions of the operating system it runs on

Troubleshoot Common Issues

Let us learn some common issues that are faced during window creation:

Window Not Showing

If your window isn’t appearing, check that you’ve:

  • Called window.show()
  • Started the event loop with app.exec()
  • Not encountered any exceptions (check your console output)

Read Handle Button Click Events Using Signals and Slots in PyQt6

Window Appears and Immediately Closes

This usually happens when your main function exits immediately. Make sure you’re calling sys.exit(app.exec()) to keep the application running.

Widgets Not Appearing

If you’ve added widgets but they don’t show up, ensure that:

  • You’ve added them to a layout
  • You’ve set the layout on a widget
  • The widget is visible

Conclusion

In this tutorial, I explained how to create a basic window in PyQt6. I discussed prerequisites, installing PyQt6, and methods to create windows in PyQt6, such as using QWidget, QMainWindow, using the object-oriented approach. I also covered customizing windows, window style and appearance, properties, creating multiple windows, best practices, and troubleshooting common issues.

Related article you may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.