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 PyQt6Create 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:
- Imports necessary modules
- Creates a QApplication instance
- Creates a QWidget (our window)
- Sets window properties
- Shows the window
- 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.

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.

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.

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, ySet 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:
| Property | Method | Description |
|---|---|---|
| Title | setWindowTitle() | Sets the window title |
| Size | resize() | Sets the window size |
| Position | move() | Sets the window position |
| Minimum Size | setMinimumSize() | Sets the minimum allowed window size |
| Maximum Size | setMaximumSize() | Sets the maximum allowed window size |
| Fixed Size | setFixedSize() | Makes the window non-resizable |
| Window Icon | setWindowIcon() | Sets the window icon |
| Window Opacity | setWindowOpacity() | 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:
- Use object-oriented programming – Create custom window classes for better organization
- Separate UI and logic – Keep your UI code separate from your business logic
- Use layouts – Always use layouts instead of absolute positioning for responsive UIs
- Consider screen resolution – Test your app on different screen sizes and resolutions
- 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:
- Arrange Widgets Using QGridLayout in PyQt6
- Use QVBoxLayout in PyQt6 for Vertical Layouts
- Use QHBoxLayout in PyQt6 for Horizontal Layouts

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.