MainWindow.py (Código)

# -*- coding: utf-8 -*-
"""Python e Qt 6: PyQt6 QToolBar()."""
from pathlib import Path
from PyQt6.QtGui import QAction, QIcon
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
# Informações do aplicativo.
APPLICATION_NAME = 'br.natorsc.Exemplo'
ORGANIZATION_NAME = APPLICATION_NAME.split('.')[1]
ORGANIZATION_DOMAIN = '.'.join(APPLICATION_NAME.split('.')[0:2])
BASE_DIR = Path(__file__).resolve().parent
ROOT_DIR = BASE_DIR.parent.parent
window_icon = str(ROOT_DIR.joinpath('assets', 'icons', 'icon-256x256.png'))
cut_icon = str(ROOT_DIR.joinpath('assets', 'icons', 'cut-128x128.png'))
lifebuoy_icon = str(ROOT_DIR.joinpath('assets', 'icons', 'lifebuoy-128x128.png'))
exit_icon = str(ROOT_DIR.joinpath('assets', 'icons', 'exit-128x128.png'))
window_title = 'Python e Qt 6: PyQt6 QToolBar().'
class MainWindow(QMainWindow):
    def __init__(self, application):
        super().__init__()
        self.application = application
        # Informações do monitor principal.
        primary_screen = self.application.primaryScreen()
        primary_screen_geometry = primary_screen.geometry()
        primary_screen_height = primary_screen_geometry.height()
        primary_screen_width = primary_screen_geometry.width()
        # Configurando a janela.
        # Definindo o tamanho da janela de forma dinâmica.
        window_width = int(primary_screen_width / 2)
        window_height = int(primary_screen_height / 2)
        # Configurando a posição e tamanho.
        self.setGeometry(0, 0, window_width, window_height)
        # Tamanho minimo da janela.
        self.setMinimumSize(window_width, window_height)
        # Tamanho maximo da janela.
        self.setMaximumSize(window_width + 200, window_height + 200)
        # Titulo da janela.
        self.setWindowTitle(window_title)
        # Ícone da janela
        self.setWindowIcon(QIcon(window_icon))
        # Layout do central_widget.
        vbox = QVBoxLayout()
        # Widget central.
        central_widget = QWidget()
        # Definindo o layout do widget central.
        central_widget.setLayout(vbox)
        # Adicionando o widget central.
        self.setCentralWidget(central_widget)
        # O seu código aqui:
        toolbar = self.addToolBar('nome-da-barra')
        copy_action = QAction('Copiar', self)
        # Definindo ícone com base no tema do sistema operacional.
        # Se o sistema operacional não tiver o ícone o texto será exibido.
        copy_action.setIcon(QIcon().fromTheme('edit-copy'))
        toolbar.addAction(copy_action)
        cut_action = QAction(QIcon(cut_icon), 'Recortar', self)
        toolbar.addAction(cut_action)
        about_action = QAction(QIcon(lifebuoy_icon), 'Sobre', self)
        toolbar.addAction(about_action)
        exit_action = QAction(QIcon(exit_icon), 'Sair', self)
        # Ação que é realizada quando o menu é clicado.
        exit_action.triggered.connect(self.exit_app)
        toolbar.addAction(exit_action)
        # Exibindo a janela.
        self.show()
    def exit_app(self):
        self.application.quit()
if __name__ == "__main__":
    import sys
    # Verificando a plataforma onde o código está sendo executado.
    if sys.platform == 'win32' or sys.platform == 'win64':
        from ctypes import windll
        # Para exibir o ícone na taskbar (barra de tarefas) do windows.
        windll.shell32.SetCurrentProcessExplicitAppUserModelID(APPLICATION_NAME)
    elif sys.platform == 'linux':
        from os import environ, getenv
        # Definindo o tipo de sessão onde o código será executado.
        if getenv('XDG_SESSION_TYPE') == 'wayland':
            environ['QT_QPA_PLATFORM'] = 'wayland'
        elif getenv('XDG_SESSION_TYPE') is None:
            # Funciona bem no Chrome OS.
            environ['QT_QPA_PLATFORM'] = 'xcb'
    application = QApplication(sys.argv)
    application.setOrganizationName(ORGANIZATION_NAME)
    application.setOrganizationDomain(ORGANIZATION_DOMAIN)
    application.setApplicationName(APPLICATION_NAME)
    application.setDesktopFileName(f'{APPLICATION_NAME}.desktop')
    window = MainWindow(application=application)
    sys.exit(application.exec())