from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QGraphicsDropShadowEffect
from PyQt5.QtCore import Qt, QPoint, QEvent
from PyQt5.QtGui import QColor
class pywidget(QWidget):
def __init__(self):
QWidget.__init__(self)
# 对象声明
self.shadowWidget = QWidget()
self.shadowLayout = QVBoxLayout()
self.shadow = QGraphicsDropShadowEffect()
self.shadowColor = QColor(0, 0, 0)
self.titleBarWidget = QWidget()
self.titleBarLayout = QHBoxLayout()
self.titleLabel = QLabel()
self.minButton = QPushButton()
self.maxButton = QPushButton()
self.quitButton = QPushButton()
self.mainLayout = QVBoxLayout()
self.mainWidget = QWidget()
# 设置窗口属性
self.setFixedSize(800, 600)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.installEventFilter(self)
self.setMouseTracking(True)
self.shadowLayout.addWidget(self.shadowWidget)
self.shadowLayout.setContentsMargins(9, 9, 9, 9)
self.shadowLayout.setSpacing(0)
self.setLayout(self.shadowLayout)
self.shadowWidget.setLayout(self.mainLayout)
self.shadowWidget.setGraphicsEffect(self.shadow)
self.shadowWidget.installEventFilter(self)
self.shadowWidget.setMouseTracking(True)
self.shadow.setBlurRadius(9)
self.shadowColor.setAlpha(100)
self.shadow.setColor(self.shadowColor)
self.shadow.setOffset(0)
# 标题栏窗体
self.titleBarWidget.setLayout(self.titleBarLayout)
self.titleBarWidget.setFixedHeight(31)
self.titleBarWidget.installEventFilter(self)
self.titleBarWidget.setMouseTracking(True)
self.titleBarWidget.setObjectName("titleBarWidget")
self.titleBarWidget.setStyleSheet("#titleBarWidget{background-color: rgb(50, 50, 50); border-top: 1px solid; border-right: 1px solid; border-left: 1px solid; \
border-color: rgba(200,200,200,50); border-top-right-radius:2px; border-top-left-radius:2px;}")
# 标题栏布局
self.titleBarLayout.setContentsMargins(0, 0, 0, 0)
self.titleBarLayout.setSpacing(0)
self.titleBarLayout.addWidget(self.titleLabel)
self.titleBarLayout.addWidget(self.minButton)
self.titleBarLayout.addWidget(self.maxButton)
self.titleBarLayout.addWidget(self.quitButton)
# 标题标签
self.titleLabel.setText(" 窗口")
self.titleLabel.setStyleSheet('QLabel{font: 10pt "微软雅黑"; color: rgb(121, 116, 127)}')
self.titleLabel.setFixedHeight(31)
self.titleLabel.setMouseTracking(True)
# 标题栏按钮
self.minButton.setFixedSize(35, 31)
self.minButton.setText("0")
self.minButton.setMouseTracking(True)
self.minButton.clicked.connect(self.minButton_clicked)
self.minButton.setStyleSheet('QPushButton {font: 10pt "Marlett"; border:none; background-color: rgba(204, 204, 204, 0); color: rgb(121, 116, 127);}\
QPushButton:hover{background-color: rgba(255, 255, 255, 10);}\
QPushButton:pressed{background-color: rgba(255, 255, 255, 20);}')
self.maxButton.setFixedSize(35, 31)
self.maxButton.setText("1")
self.maxButton.setMouseTracking(True)
self.maxButton.clicked.connect(self.maxButton_clicked)
self.maxButton.setStyleSheet('QPushButton {font: 10pt "Marlett"; background-color: rgba(204, 204, 204, 0); color: rgb(121, 116, 127); border-top: 2px solid; border-color: rgba(200,200,200,0);}\
QPushButton:hover{background-color: rgba(255, 255, 255, 10);}\
QPushButton:pressed{background-color: rgba(255, 255, 255, 20);}')
self.quitButton.setFixedSize(35, 31)
self.quitButton.setText("r")
self.quitButton.setMouseTracking(True)
self.quitButton.clicked.connect(self.quitButton_clicked)
self.quitButton.setStyleSheet('QPushButton {font: 10pt "Marlett"; background-color: rgba(204, 204, 204, 0); color: rgb(121, 116, 127); border-top-right-radius: 2px; \
border-top: 1px solid; border-right: 1px solid; border-color: rgba(200,200,200,0);}\
QPushButton:hover{background-color: rgb(244, 84, 84); color: rgb(255, 255, 255); border-color: rgba(200,200,200,50);} \
QPushButton:pressed{background-color: rgb(220, 72, 72);}')
self.mainLayout.setContentsMargins(0, 0, 0, 0)
self.mainLayout.setSpacing(0)
self.mainLayout.addWidget(self.titleBarWidget)
self.mainLayout.addWidget(self.mainWidget)
self.mainWidget.setMouseTracking(True)
self.mainWidget.setStyleSheet("QWidget{background-color: rgb(57, 58, 60); border-bottom: 1px solid; border-right: 1px solid; border-left: 1px solid; \
border-color: rgba(200,200,200,50); border-bottom-right-radius:2px; border-bottom-left-radius:2px;}")
self.dragMode = ''
def eventFilter(self, watched, event):
# 监听标题栏事件
#print("watched", event.type())
if watched == self.titleBarWidget:
if event.type() == QEvent.MouseButtonDblClick:
if self.isMaximized():
self.showNormal()
else:
self.showMaximized()
if event.type() == QEvent.MouseButtonPress:
self.clickPos = event.globalPos() - self.pos()
if event.type() == QEvent.MouseMove:
if event.buttons() == Qt.LeftButton and self.clickPos and not self.isMaximized():
self.move(event.globalPos() - self.clickPos)
elif event.buttons() == Qt.LeftButton and self.clickPos and self.isMaximized():
maxWidth = self.width()
self.showNormal()
if self.width() - maxWidth + self.clickPos.x() <= 0 and self.width() - self.clickPos.x() - 35 * 3 <= 0:
self.clickPos.setX(self.width()/2)
elif self.width() - maxWidth + self.clickPos.x() > 0:
self.clickPos.setX(self.width() - maxWidth + self.clickPos.x())
elif self.width() - self.clickPos.x() - 35 * 3 > 0:
self.clickPos.setX(self.clickPos.x()+9)
self.clickPos.setY(self.clickPos.y() + 11)
if event.type() == QEvent.MouseButtonRelease:
self.clickPos = QPoint()
if watched == self:
if event.type() == QEvent.MouseButtonPress:
self.dragPos = event.globalPos()
self.dragWidth = self.width()
self.dragHeight = self.height()
if not self.isMaximized():
self.mousePos = event.globalPos() - self.pos()
if self.width() - 10 <= self.mousePos.x() <= self.width() - 5:
if self.height() - 10 <= self.mousePos.y() <= self.height() - 5:
self.dragMode = 'rb'
elif 5 <= self.mousePos.y() <= 10:
self.dragMode = 'rt'
else:
self.dragMode = 'r'
elif 5 <= self.mousePos.x() <= 10:
if self.height() - 10 <= self.mousePos.y() <= self.height() - 5:
self.dragMode = 'lb'
elif 5 <= self.mousePos.y() <= 10:
self.dragMode = 'lt'
else:
两只程序猿
- 粉丝: 271
- 资源: 82
- 1
- 2
前往页