To create a customized title bar that is frameless using PyInstaller and custom widgets for QT-PyQt-PySide, you can follow these steps:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QIcon, QMouseEvent
from PyQt5.QtCore import Qt, QRect, QSize
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;
}
""")
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)
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()
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)
pyinstaller --noconfirm --onefile --windowed --icon=app.ico --name=CustomTitleBar main.py
Note: You can modify the appearance of the title bar by changing the style sheet.
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
Asked: 2023-05-30 14:14:06 +0000
Seen: 21 times
Last updated: May 30 '23
How can one ensure that sub-classes have uniform method parameters in TypeScript?
How can the calculation of matrix determinant be performed using CUDA?
How can code repetition be prevented when using (box)plot functions?
What steps can I take to prevent my webpage from slowing down when all parts of a div are displayed?
How can circles be detected in openCV?
What is the method to determine the most precise categorization of data using Self Organizing Map?