<Pyqt5> PyQt5自定义标题栏

class MyBar(QWidget):

    def get_button(self, icon, onClick):
        btn_size = 35
        btn = borderless_tool_button(QIcon(icon), size=ThpQSize(16, 16))
        btn.clicked.connect(onClick)
        btn.setFixedSize(btn_size, btn_size)
        btn.setStyleSheet("QToolButton { border:None; background:transparent; } ")
        return btn

    def __init__(self, parent):
        super(MyBar, self).__init__()
        self.parent = parent
        print(self.parent.width())
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.title = QLabel("My Own Bar")

        self.btn_close = self.get_button(":/res/close.png", self.btn_close_clicked)
        self.btn_min = self.get_button(":/res/minimize.png", self.btn_min_clicked)
        self.btn_max = self.get_button(":/res/maximize.png", self.btn_max_clicked)

        self.title.setFixedHeight(35)
        self.title.setAlignment(Qt.AlignCenter)
        self.layout.addWidget(self.title)
        self.layout.addWidget(self.btn_min)
        self.layout.addWidget(self.btn_max)
        self.layout.addWidget(self.btn_close)

        self.title.setStyleSheet("""
            background-color: #171745;
            color: white;
        """)
        self.setLayout(self.layout)
        self.title.setFont(yh_font(11))

        self.start = QPoint(0, 0)
        self.pressing = False

    def resizeEvent(self, QResizeEvent):
        super(MyBar, self).resizeEvent(QResizeEvent)
        self.title.setFixedWidth(self.parent.width())

    def mousePressEvent(self, event):
        self.start = self.mapToGlobal(event.pos())
        self.pressing = True

    def mouseMoveEvent(self, event):
        if self.pressing:
            self.end = self.mapToGlobal(event.pos())
            self.movement = self.end-self.start
            self.parent.setGeometry(self.mapToGlobal(self.movement).x(),
                                self.mapToGlobal(self.movement).y(),
                                self.parent.width(),
                                self.parent.height())
            self.start = self.end

    def mouseReleaseEvent(self, QMouseEvent):
        self.pressing = False

    def btn_close_clicked(self):
        self.parent.close()

    def btn_max_clicked(self):
        self.parent.showMaximized()

    def btn_min_clicked(self):
        self.parent.showMinimized()

class DemoWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setMinimumSize(1160, 800)
        self.setStyleSheet("background-color: #205E86")
        self.titleBar = MyBar(self)
        layout = QtWidgets.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 10)
        layout.setSpacing(0)
        layout.addWidget(self.titleBar)
        layout.addStretch()
        self.setLayout(layout)
def set_dark_theme(app: QApplication):
    palette = QPalette()
    palette.setColor(QPalette.Window, QColor(53, 53, 53))
    palette.setColor(QPalette.WindowText, Qt.white)
    palette.setColor(QPalette.Base, QColor(25, 25, 25))
    palette.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
    palette.setColor(QPalette.ToolTipBase, Qt.black)
    palette.setColor(QPalette.ToolTipText, Qt.white)
    palette.setColor(QPalette.Text, Qt.white)
    palette.setColor(QPalette.Button, QColor(53, 53, 53))
    palette.setColor(QPalette.ButtonText, Qt.white)
    palette.setColor(QPalette.BrightText, Qt.red)
    palette.setColor(QPalette.Link, QColor(42, 130, 218))
    palette.setColor(QPalette.Highlight, QColor(42, 130, 218))
    palette.setColor(QPalette.HighlightedText, Qt.black)
    app.setPalette(palette)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    set_dark_theme(app)
    window = DemoWidget()
    window.show()
    sys.exit(app.exec_())


<Pyqt5> PyQt5自定义标题栏_第1张图片

你可能感兴趣的:(Windows,pyqt,qt,python,开发语言)