pyqt5一个基本的无边框标签栏

import sys
from PyQt5.Qt import *
from PyQt5.QtCore import *
from PyQt5 import QtGui
from PyQt5.QtWidgets import *


class FramelessWindow(QWidget):

    def __init__(self):
        super(FramelessWindow, self).__init__()
        self.setMaximumSize(1920, 1080)
        global win_min_x
        win_min_x = 1200
        global win_min_y
        win_min_y = 800
        self.setMinimumSize(win_min_x, win_min_y)
        self.layout = QVBoxLayout()
        self.mybar = MyBar(self)
        self.layout.addWidget(self.mybar)
        self.layout.addStretch(1)  # 间隔处理
        self.use_palette()
        self.layout.addStretch(1)
        self.setLayout(self.layout)
        self.layout.setContentsMargins(0, 0, 0, 0)  # left top right down
        self.setWindowFlags(Qt.FramelessWindowHint)  # 去边框
        self.mybar.windowMinimumed.connect(lambda: self.onClick_Button_min())  # 信号槽 收到mybar的信号 执行内部函数
        self.mybar.windowMaximuned.connect(lambda: self.onClick_Button_max())

    def use_palette(self):  # 背景图片
        palette = QtGui.QPalette()
        pix = QtGui.QPixmap("")
        pix = pix.scaled(self.width(), self.height())
        palette.setBrush(self.backgroundRole(),
                         QtGui.QBrush(pix))
        self.setPalette(palette)

    def resizeEvent(self, event):  # 背景图拉伸
        palette = QtGui.QPalette()
        pix = QtGui.QPixmap("")
        pix = pix.scaled(self.width(), self.height())
        palette.setBrush(QtGui.QPalette.Background, QtGui.QBrush(pix))
        self.setPalette(palette)

    def onClick_Button_min(self):
        print('最小化按钮被按下')
        self.showMinimized()

    def onClick_Button_max(self):
        print('最大化、normal按钮被按下')
        if self.mybar.btn_max_flag == 0:
            self.mybar.btn_max.setText('normal')
            self.mybar.btn_max_flag = 1
            self.showMaximized()
            print('最大化')
        elif self.mybar.btn_max_flag == 1:
            self.mybar.btn_max.setText('max')
            self.mybar.btn_max_flag = 0
            self.showNormal()
            print('normal')


class MyBar(QWidget):
    windowMinimumed = pyqtSignal()  # 最小化信号 可含参
    windowMaximuned = pyqtSignal()  # 最大化
    windowNormaled = pyqtSignal()  # Normal
    btn_max_flag = 0

    def __init__(self, parent):
        super(MyBar, self).__init__()
        self.parent = parent
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.title = QLabel("Test_Title")
        self.title.setFixedHeight(35)
        self.title.setAlignment(Qt.AlignCenter)

        self.btn_quit = QPushButton('quit')  # quit button
        self.btn_quit.clicked.connect(lambda: self.onClick_Button_quit())  # 按钮信号与函数槽

        self.btn_min = QPushButton('min')  # minimize button
        self.btn_min.clicked.connect(lambda: self.windowMinimumed.emit())  # 点击按钮发射信号

        self.btn_max = QPushButton('max')  # minimize button
        self.btn_max.clicked.connect(lambda: self.windowMaximuned.emit())

        self.layout.addWidget(self.title)
        self.layout.addWidget(self.btn_min)
        self.layout.addWidget(self.btn_max)
        self.layout.addWidget(self.btn_quit)

        self.title.setStyleSheet(""" 
      background-color: black; 
      color: white;
     """)
        self.setLayout(self.layout)

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

    def onClick_Button_quit(self):
        print('quit按钮被按下')
        app = QApplication.instance()
        app.quit()

    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 and self.btn_max_flag == 0:
            self.end = self.mapToGlobal(event.pos())
            self.movement = self.end - self.start
            self.parent.move(self.mapToGlobal(self.movement))
            self.start = self.end
        elif self.btn_max_flag == 1:
            self.end = self.mapToGlobal(event.pos())  # 测量必要
            self.movement = self.end - self.start  # 测量是否应该normal化
            if self.movement.y() > 10 or self.movement.y() < -10:  # max态下 拖动转为normal
                self.parent.showNormal()
                self.btn_max_flag = 0
                self.gx = event.globalX()
                self.oppx = int(self.gx / 1920 * win_min_x)  # opposite X 计算合理相对位置
                print(self.oppx)
                self.parent.move(self.gx - self.oppx, event.y() - 20)

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

    def mouseDoubleClickEvent(self, event):
        super(MyBar, self).mouseDoubleClickEvent(event)
        if self.parent.windowState() == Qt.WindowNoState:  # normal
            self.windowMaximuned.emit()
        elif self.parent.windowState() == Qt.WindowMaximized:
            self.windowMaximuned.emit()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = FramelessWindow()
    mw.setWindowTitle('test test')
    mw.show()
    sys.exit(app.exec_())

//最近在学习pyqt5 记录一下
//侵删
实现了一点方便的功能

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页