Ask Your Question
0

How can I create a customized title bar that is frameless using PyInstaller and custom widgets for QT-PyQt-PySide?

asked 2023-05-30 14:14:06 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-30 14:32:02 +0000

ladyg gravatar image

To create a customized title bar that is frameless using PyInstaller and custom widgets for QT-PyQt-PySide, you can follow these steps:

  1. Import the necessary modules:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QIcon, QMouseEvent
from PyQt5.QtCore import Qt, QRect, QSize
  1. Create a class for the title bar widget:
class TitleBar(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.title = "Custom Title Bar"
        self.setFixedHeight(28)
        self.iconSize = QSize(18, 18)
        self.setStyleSheet("""
            QWidget {
                background-color: #242424;
            }
            QLabel {
                color: #ffffff;
            }
            QPushButton {
                background-color: transparent;
                border: none;
                color: #ffffff;
            }
            QPushButton:hover {
                color: #21a0d7;
            }
            QPushButton:pressed {
                color: #1477b9;
            }
        """)
  1. Add the necessary widgets to the title bar:
        layout = QHBoxLayout(self)
        layout.setSpacing(0)
        layout.setContentsMargins(0, 0, 0, 0)

        self.titleLabel = QLabel(self.title)
        self.titleLabel.setFixedHeight(self.height())
        layout.addWidget(self.titleLabel)

        layout.addStretch()

        self.minimizeButton = QPushButton("_")
        self.minimizeButton.setIconSize(self.iconSize)
        self.minimizeButton.setFixedHeight(self.height())
        layout.addWidget(self.minimizeButton)

        self.maximizeButton = QPushButton("O")
        self.maximizeButton.setIconSize(self.iconSize)
        self.maximizeButton.setFixedHeight(self.height())
        layout.addWidget(self.maximizeButton)

        self.closeButton = QPushButton("X")
        self.closeButton.setIconSize(self.iconSize)
        self.closeButton.setFixedHeight(self.height())
        layout.addWidget(self.closeButton)
  1. Implement the mouse event functions to enable dragging the window:
    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self._dragging = True
            self._lastPos = event.pos()
            event.accept()

    def mouseMoveEvent(self, event: QMouseEvent):
        if self._dragging:
            self.parent().move(self.parent().pos() + event.pos() - self._lastPos)
            event.accept()

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self._dragging = False
            event.accept()
  1. Create the main window widget and layout:
class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setFixedSize(600, 400)
        self.setLayout(QVBoxLayout())
        self.layout().setSpacing(0)
        self.layout().setContentsMargins(0, 0, 0, 0)

        titleBar = TitleBar(self)
        self.layout().addWidget(titleBar)
  1. Compile the application using PyInstaller:
pyinstaller --noconfirm --onefile --windowed --icon=app.ico --name=CustomTitleBar main.py
  1. Run the application and enjoy the customized frameless window with a title bar.

Note: You can modify the appearance of the title bar by changing the style sheet.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-30 14:14:06 +0000

Seen: 21 times

Last updated: May 30 '23