How to Create Icons for Windows in PyQt6?

In this article, I will explain how to create icons for windows in PyQt6. As a Python developer who worked on various projects using PyQt6, I have learned that icons are not just decorative, they are functional elements that enhance usability and brand recognition. Let us learn about creating and implementing icons in PyQt6 applications along with examples.

Basic Window Icon Implementation in PyQt6

The most fundamental icon in any application is the window icon—the image that appears in the taskbar, window title bar, and Alt+Tab switcher. Here’s how to set it:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtGui import QIcon

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("My Application")
        self.setGeometry(100, 100, 800, 600)
        
        # Set the window icon
        self.setWindowIcon(QIcon('app_icon.png'))
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

Refer to the screenshot below to see the output window.

Create Icons for Windows in PyQt6

This simple approach uses the setWindowIcon method to set your application’s main icon. The icon will appear in the window’s title bar and the taskbar when the application is running.

Read  Handle Button Click Events Using Signals and Slots in PyQt6

Icon File Formats and Recommendations

When creating icons for PyQt6 applications, you have several format options. Here’s my recommended approach based on years of development experience:

Recommended Icon Formats

  1. PNG – Best for most icons due to transparency support and wide compatibility
  2. SVG – Excellent for scalable icons that look crisp at any size
  3. ICO – Windows-specific format that can contain multiple sizes
  4. JPG – Avoid icons due to lack of transparency

For optimal compatibility across Windows, macOS, and Linux, I recommend preparing your application icon in multiple sizes:

Size (pixels)Usage
16×16Small taskbar icons, favicons
32×32Standard taskbar and file icons
48×48Desktop icons
64×64High-DPI displays
128×128App stores, Start menu
256×256Modern high-resolution displays

Check out Arrange Widgets Using QGridLayout in PyQt6

Advanced Icon Implementation with Qt Resource System

While directly loading icon files works for simple applications, professional PyQt6 applications should use Qt’s resource system. This approach embeds icons directly into your executable, making deployment much easier.

Here’s how to implement it:

Step 1: Create a .qrc Resource File

Create a file named resources.qrc:

<!DOCTYPE RCC>
<RCC version="1.0">
    <qresource prefix="/icons">
        <file>icons/app_icon.png</file>
        <file>icons/save.png</file>
        <file>icons/open.png</file>
        <file>icons/new.png</file>
        <file>icons/exit.png</file>
    </qresource>
</RCC>

Step 2: Compile the Resource File

Use the pyrcc6 tool to compile your resource file:

pyrcc6 -o resources_rc.py resources.qrc

Step 3: Import and Use the Resource File

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QToolBar, QAction
from PyQt6.QtGui import QIcon
import resources_rc  # Import the compiled resource file

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle("Document Editor Pro")
        self.setGeometry(100, 100, 800, 600)
        
        # Use icon from resource file
        self.setWindowIcon(QIcon(':/icons/app_icon.png'))
        
        # Create toolbar with icons from resources
        toolbar = QToolBar("Main Toolbar")
        self.addToolBar(toolbar)
        
        new_action = QAction(QIcon(':/icons/new.png'), "New", self)
        toolbar.addAction(new_action)
        
        open_action = QAction(QIcon(':/icons/open.png'), "Open", self)
        toolbar.addAction(open_action)
        
        save_action = QAction(QIcon(':/icons/save.png'), "Save", self)
        toolbar.addAction(save_action)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

Using the Qt resource system offers several advantages:

  • Single executable deployment: No need to distribute icon files separately
  • Guaranteed availability: Resources are always available, eliminating missing file errors
  • Improved performance: Resources are loaded efficiently
  • Path independence: No need to worry about relative file paths

This approach is what I use for all commercial applications I develop for clients in New York, San Francisco, and across the United States.

Read Use QVBoxLayout in PyQt6 for Vertical Layouts

Create Icon Sets with Theme Support

For applications that need to support different themes or color modes (increasingly common in American software), PyQt6 supports themed icons:

from PyQt6.QtWidgets import QApplication, QMainWindow, QToolBar, QAction
from PyQt6.QtGui import QIcon

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        # Use standard icon from the current theme
        new_action = QAction(QIcon.fromTheme("document-new"), "New", self)
        
        # Fall back to custom icon if theme icon is not available
        open_action = QAction(QIcon.fromTheme("document-open", 
                                             QIcon(':/icons/open.png')), "Open", self)

The fromTheme method lets you use standard icon names that adapt to the user’s system theme. This is especially useful for cross-platform applications that should respect the user’s desktop environment.

Use Built-in Qt Icons

Qt comes with a set of standard icons that you can use in your applications. These adapt to the platform style automatically:

from PyQt6.QtWidgets import QApplication, QMainWindow, QToolBar, QAction, QStyle

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        # Use standard style icons
        style = self.style()
        
        save_icon = style.standardIcon(QStyle.StandardPixmap.SP_DialogSaveButton)
        save_action = QAction(save_icon, "Save", self)
        
        help_icon = style.standardIcon(QStyle.StandardPixmap.SP_DialogHelpButton)
        help_action = QAction(help_icon, "Help", self)

This approach ensures your application follows platform conventions, which is particularly important for American users who expect software to follow Windows UI guidelines.

Check out Use QListWidget in PyQt6

Create Custom Icon Sets with QIcon

For applications with specialized needs, you can create multi-resolution icons using QIcon’s addPixmap or addFile methods:

from PyQt6.QtGui import QIcon, QPixmap
from PyQt6.QtCore import QSize

# Create a multi-resolution icon
app_icon = QIcon()
app_icon.addPixmap(QPixmap("icon16.png"), QIcon.Mode.Normal, QIcon.State.Off, QSize(16, 16))
app_icon.addPixmap(QPixmap("icon32.png"), QIcon.Mode.Normal, QIcon.State.Off, QSize(32, 32))
app_icon.addPixmap(QPixmap("icon64.png"), QIcon.Mode.Normal, QIcon.State.Off, QSize(64, 64))

# Set as application icon
app.setWindowIcon(app_icon)

This approach allows you to provide the best-looking icon for each display resolution and scaling factor.

Create Dynamic Icons with SVG and Web Technologies

For modern applications that need highly dynamic icons, PyQt6 offers excellent SVG support:

from PyQt6.QtSvg import QSvgRenderer
from PyQt6.QtGui import QPainter, QPixmap, QIcon, QColor
from PyQt6.QtCore import Qt

def create_colored_icon(svg_path, color, size=32):
    """Create an icon from SVG with custom color"""
    renderer = QSvgRenderer(svg_path)
    pixmap = QPixmap(size, size)
    pixmap.fill(Qt.GlobalColor.transparent)
    painter = QPainter(pixmap)
    renderer.render(painter)
    painter.end()
    return QIcon(pixmap)

# Use the function
red_icon = create_colored_icon("icon.svg", QColor("#FF0000"))
blue_icon = create_colored_icon("icon.svg", QColor("#0000FF"))

For even more advanced icon capabilities, you can use QWebEngineView to render icons using web technologies:

from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl

# Create a web view to render an icon using web technologies
web_view = QWebEngineView()
web_view.setUrl(QUrl("file:///path/to/icon-renderer.html"))

This approach allows you to create highly dynamic icons using HTML, CSS, and JavaScript.

Read Create QLineEdit Widget in PyQt6

Icon Generator Application Example

Let’s put everything together into a practical example: a simple icon generator application. This demonstrates how to create and use icons in a real-world PyQt6 application:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QFileDialog, QVBoxLayout, QWidget
from PyQt6.QtGui import QPixmap
from PIL import Image  # Requires Pillow library for image conversion

class IconGeneratorApp(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Icon Generator")
        self.setGeometry(100, 100, 400, 300)

        self.initUI()

    def initUI(self):
        # Main layout
        layout = QVBoxLayout()

        # Label to display image preview
        self.image_label = QLabel("No image selected")
        self.image_label.setStyleSheet("border: 1px solid gray; padding: 10px;")
        self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(self.image_label)

        # Button to load image
        self.btn_load = QPushButton("Select Image")
        self.btn_load.clicked.connect(self.load_image)
        layout.addWidget(self.btn_load)

        # Button to convert image to icon
        self.btn_convert = QPushButton("Convert to Icon (.ico)")
        self.btn_convert.clicked.connect(self.convert_to_icon)
        self.btn_convert.setEnabled(False)  # Disable until image is loaded
        layout.addWidget(self.btn_convert)

        # Set central widget
        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def load_image(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "Select Image", "", "Images (*.png *.jpg *.jpeg *.bmp)")
        if file_path:
            self.image_path = file_path
            pixmap = QPixmap(file_path)
            self.image_label.setPixmap(pixmap.scaled(150, 150))
            self.btn_convert.setEnabled(True)  # Enable conversion button

    def convert_to_icon(self):
        save_path, _ = QFileDialog.getSaveFileName(self, "Save Icon", "", "Icon Files (*.ico)")
        if save_path:
            image = Image.open(self.image_path)
            image.save(save_path, format="ICO")
            self.image_label.setText("Icon saved successfully!")

# Run application
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = IconGeneratorApp()
    window.show()
    sys.exit(app.exec())

Conclusion

In this article, I explained how to create icons for windows in PyQt6. I discussed basic window icon implementation in PyQt, icon file format and recommendations, advanced icon implementation with the Qt resources system, creating icon sets with theme support, and using built-in Qt Icons. I also covered creating custom icon sets with QIcon, creating dynamic icons with SVG and web technologies, and icon generator application example.

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.