上一篇文章链接:
PySide6 完整的自定义【边框】窗口框架代码-CSDN博客https://blog.csdn.net/2402_84665876/article/details/141152359
简述:
自定义边框窗口入口函数可接受的参数:
class WindowBorder(QMainWindow):
def __init__(self, parent: QWidget | None = None,
taskbar_height: int = 45,
MinimumSize: QSize = QSize(400, 225),
ReSize: QSize = QSize(400, 225),
Position: QPoint | None = None,
title_height: int = 35,
title_font_size: int = 15,
icon: QIcon = None,
title: str = "",
background_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (230, 230, 230, 1),
title_background_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (0, 0, 0, 0.8),
title_text_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (255, 255, 255, 1),
button_hoverColor: tuple[int, int, int, int] | list[int] | set[int] = (100, 100, 100, 1),
closeButton_hoverColor: tuple[int, int, int, int] | list[int] | set[int] = (255, 0, 0, 1),
glass_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (240, 240, 240, 0.5),
glass_border_color_rgb: tuple[int, int, int] | list[int] | set[int] = (125, 125, 125)):
pass
调用示例:
from PySide6.QtCore import QSize
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication
from windowBorder import WindowBorder
from sys import argv
from sys import exit as sys_exit
app = QApplication(argv)
window = WindowBorder(MinimumSize=QSize(100, 70), title="123", icon=QIcon("icon.png"))
sys_exit(app.exec())
完整代码:
windowBorder.py
from PySide6.QtCore import (Qt,
QPoint,
QSize,
QRect,
QPropertyAnimation,
QParallelAnimationGroup)
from PySide6.QtGui import (QIcon,
QPixmap,
QCursor, QGuiApplication)
from PySide6.QtWidgets import (QMainWindow,
QWidget,
QPushButton,
QLabel)
from customizeWindow import CustomizeWindow
class WindowBorder(QMainWindow):
def __init__(self, parent: QWidget | None = None,
taskbar_height: int = 45,
MinimumSize: QSize = QSize(400, 225),
ReSize: QSize = QSize(400, 225),
Position: QPoint | None = None,
title_height: int = 35,
title_font_size: int = 15,
icon: QIcon = None,
title: str = "",
background_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (230, 230, 230, 1),
title_background_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (0, 0, 0, 0.8),
title_text_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (255, 255, 255, 1),
button_hoverColor: tuple[int, int, int, int] | list[int] | set[int] = (100, 100, 100, 1),
closeButton_hoverColor: tuple[int, int, int, int] | list[int] | set[int] = (255, 0, 0, 1),
glass_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (240, 240, 240, 0.5),
glass_border_color_rgb: tuple[int, int, int] | list[int] | set[int] = (125, 125, 125)):
super(WindowBorder, self).__init__(parent=parent)
self.setWindowFlags(Qt.WindowType.Window |
Qt.WindowType.FramelessWindowHint |
Qt.WindowType.WindowSystemMenuHint |
Qt.WindowType.WindowMinimizeButtonHint |
Qt.WindowType.WindowMaximizeButtonHint |
Qt.WindowType.WindowCloseButtonHint)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
self.glass_color_rgba = glass_color_rgba
self.glass_border_color_rgb = glass_border_color_rgb
self.Min = MinimumSize
self.__setGlass()
self.body = CustomizeWindow(parent=self,
taskbar_height=taskbar_height,
MinimumSize=MinimumSize,
ReSize=ReSize,
Position=Position,
title_height=title_height,
title_font_size=title_font_size,
icon=icon,
title=title,
background_color_rgba=background_color_rgba,
title_background_color_rgba=title_background_color_rgba,
title_text_color_rgba=title_text_color_rgba,
button_hoverColor=button_hoverColor,
closeButton_hoverColor=closeButton_hoverColor)
super(WindowBorder, self).show()
def showNormal(self):
self.showMaximized()
def showFullScreen(self):
self.showMaximized()
def showMinimized(self):
if self.isMinimized():
self.showMaximized()
self.body.show()
else:
self.body.showMinimized()
def showMin(self):
super().showMinimized()
def show(self):
if self.isMinimized():
self.showMaximized()
self.body.show()
else:
self.body.showMinimized()
def __setGlass(self):
self.glass = QPushButton(self)
self.glass.setStyleSheet("QPushButton {"
f"background-color:rgba({self.glass_color_rgba[0]}, {self.glass_color_rgba[1]}, {self.glass_color_rgba[2]}, {self.glass_color_rgba[3]});"
f"border:rgb({self.glass_border_color_rgb[0]}, {self.glass_border_color_rgb[1]}, {self.glass_border_color_rgb[2]});"
"}")
self.glass.setGeometry(-10, -10, 0, 0)
def __is_glass_closed(self) -> bool:
if (self.glass.size().width() == 0 and
self.glass.size().height() == 0 and
self.glass.pos().x() == -10 and
self.glass.pos().y() == -10):
return True
else:
return False
def __glass_setGeo(self, geo: QRect):
if self.__is_glass_closed():
self.am0 = QPropertyAnimation(self.glass, b'geometry')
self.am0.setDuration(50)
self.am0.setStartValue(QRect(QCursor.pos().x(), QCursor.pos().y(), 0, 0))
self.am0.setEndValue(geo)
self.am0.start()
def event():
self.ag1.stop()
self.ag1.clear()
self.ag1 = QParallelAnimationGroup(self)
self.am0 = QPropertyAnimation(self.glass, b'geometry')
self.am0.setDuration(50)
self.am0.setStartValue(self.glass.geometry())
self.am0.setEndValue(geo)
self.ag1.addAnimation(self.am0)
self.ag1.finished.connect(event)
self.ag1.start()
def will_full_right(self):
self.__glass_setGeo(QRect(QGuiApplication.primaryScreen().size().width() // 2, 0,
QGuiApplication.primaryScreen().size().width() // 2,
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height)))
def will_full_left(self):
self.__glass_setGeo(QRect(0, 0,
QGuiApplication.primaryScreen().size().width() // 2,
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height)))
def will_up_left(self):
self.__glass_setGeo(QRect(0, 0,
QGuiApplication.primaryScreen().size().width() // 2,
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height) // 2))
def will_down_left(self):
self.__glass_setGeo(QRect(0, (QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height) // 2,
QGuiApplication.primaryScreen().size().width() // 2,
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height) // 2))
def will_up_right(self):
self.__glass_setGeo(QRect(QGuiApplication.primaryScreen().size().width() // 2, 0,
QGuiApplication.primaryScreen().size().width() // 2,
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height) // 2))
def will_down_right(self):
self.__glass_setGeo(QRect(QGuiApplication.primaryScreen().size().width() // 2,
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height) // 2,
QGuiApplication.primaryScreen().size().width() // 2,
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height) // 2))
def will_lined(self):
self.__glass_setGeo(QRect(self.body.pos().x(), 0, self.body.size().width(),
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height)))
def will_max(self):
self.__glass_setGeo(QRect(0, 0, QGuiApplication.primaryScreen().size().width(),
(QGuiApplication.primaryScreen().size().height() - self.body.taskbar_height)))
def will_close(self):
def __clo():
self.glass.setGeometry(-10, -10, 0, 0)
self.glass.setWindowOpacity(0.5)
__clo()
def will_normal(self):
self.__glass_setGeo(self.body.normal_geometry)
def will_center(self):
self.__glass_setGeo(QRect(((self.body.__screen__ - self.body.size()).width()) // 2,
(
self.body.__screen__.height() - self.body.size().height() - self.body.taskbar_height) // 2,
self.body.size().width(), self.body.size().height()))
customizeWindow.py
from PySide6.QtCore import (Qt,
QRect,
QSize,
QPoint,
QPropertyAnimation,
QParallelAnimationGroup)
from PySide6.QtGui import (QPixmap,
QMouseEvent,
QCursor,
QGuiApplication,
QIcon,
QAction)
from PySide6.QtWidgets import (QPushButton,
QWidget,
QMenu)
class CustomizeWindow(QPushButton):
edge: int
def __NoneEvent(self, event: QMouseEvent | None = None):
pass
def __attributeInit(self):
self.movemode = 0
self.is_mouse_pressed = False
self.up_resized = False
self.toDownPress = False
self.left_resized = False
self.toRightPress = False
self.upleft_resized = False
self.upright_resized = False
self.downright_resized = False
self.downleft_resized = False
self.start = False
self.edge = 5
self.screen_border = 10
self.beginning_pos_right = QCursor().pos()
self.beginning_pos_up = QCursor().pos()
self.beginning_pos_down = QCursor().pos()
self.setMouseTracking(True)
self.__setWindowStatusMenu()
self.__screen__ = QGuiApplication.primaryScreen().size()
self.animationprocessing = False
def __init__(self, parent: QWidget | None = None,
taskbar_height: int = 40,
MinimumSize: QSize = QSize(400, 225),
ReSize: QSize = QSize(400, 225),
Position: QPoint | None = None,
title_height: int = 35,
title_font_size: int = 15,
icon: QIcon = None,
title: str = "",
background_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (230, 230, 230, 1),
title_background_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (0, 0, 0, 0.8),
title_text_color_rgba: tuple[int, int, int, int] | list[int] | set[int] = (255, 255, 255, 1),
button_hoverColor: tuple[int, int, int, int] | list[int] | set[int] = (100, 100, 100, 1),
closeButton_hoverColor: tuple[int, int, int, int] | list[int] | set[int] = (255, 0, 0, 1)):
super(CustomizeWindow, self).__init__(parent)
self.__attributeInit()
self.setMinimumSize(MinimumSize)
self.__startMin = MinimumSize
self.resize(ReSize)
self.__startSize = ReSize
self.taskbar_height = taskbar_height
self.button_hoverColor = button_hoverColor
self.closeButton_hoverColor = closeButton_hoverColor
self.title_font_size = title_font_size
self.parent_name = parent
self.background_color_rgba = background_color_rgba
self.title_background_color_rgba = title_background_color_rgba
self.title_height = title_height
self.title_text_color_rgba = title_text_color_rgba
self.setStyleSheet("QPushButton {"
f"background-color: rgba({background_color_rgba[0]},{background_color_rgba[1]},{background_color_rgba[2]},{background_color_rgba[3]});"
"border: rgba(178,178,178,1);"
"}")
self.title = ""
self.setWindowTitle(title)
if icon is not None:
self.setWindowIcon(icon)
self.__setMainWidget()
self.__setThreeButton()
if Position is not None:
self.move(Position)
else:
self.move((self.__screen__.width() - self.size().width()) // 2,
(self.__screen__.height() - self.size().height() - self.taskbar_height) // 2)
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
self.parent_name.showMaximized()
self.setWindowShadow()
self.__geo__ = self.geometry()
def setWindowShadow(self):
pass
def setWindowTitle(self, arg__1):
self.parent_name.setWindowTitle(arg__1)
self.title = arg__1
self.__setWindowTitleBar(background_color_rgba=self.title_background_color_rgba,
color_rgba=self.title_text_color_rgba)
def setWindowIcon(self, icon: QIcon | QPixmap):
def windowIconDoubleClickedEvent(event: QMouseEvent):
self.closeEvent()
self.icon_image = QPushButton(self)
self.icon_image.setIcon(icon)
self.icon_image.setIconSize(QSize(self.title_height - 10, self.title_height - 10))
self.icon_image.setFixedSize(self.title_height - 10, self.title_height - 10)
self.icon_image.move(5, 5)
self.icon_image.setStyleSheet("QPushButton {"
"background-color: rgba(0,0,0,0);"
"border: none;"
"}")
self.icon_image.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.icon_image.customContextMenuRequested.connect(self.__showWindowStatusMenuFromIcon)
self.icon_image.mouseDoubleClickEvent = windowIconDoubleClickedEvent
self.parent_name.setWindowIcon(icon)
def resizeEvent(self, event):
self.WindowTitleBar.setGeometry(self.title_height + 5, 0, self.size().width(), self.title_height)
self.MoveableArea.setGeometry(0, 0, self.size().width(), self.title_height)
self.ToUpArea.setGeometry(0, 0, self.size().width(), self.edge)
self.ToDownArea.setGeometry(0, self.size().height() - self.edge, self.size().width(), self.edge)
self.ToLeftArea.setGeometry(0, 0, self.edge, self.size().height())
self.ToRightArea.setGeometry(self.size().width() - self.edge, 0, self.edge, self.size().height())
self.main_widget.setGeometry(0,
self.title_height,
self.size().width(),
self.size().height())
self.UpRightArea.setGeometry(self.size().width() - self.edge, 0, self.edge, self.edge)
self.DownRightArea.setGeometry(self.size().width() - self.edge, self.size().height() - self.edge, self.edge,
self.edge)
self.DownLeftArea.setGeometry(0, self.size().height() - self.edge, self.edge,
self.edge)
self.CloseButton.move(self.size().width() - self.CloseButton.size().width(), 0)
self.MaximumButton.move(self.CloseButton.pos().x() - self.MaximumButton.size().width(), 0)
self.MinimumButton.move(self.MaximumButton.pos().x() - self.MinimumButton.size().width(), 0)
self.CloseButton.raise_()
self.MaximumButton.raise_()
self.MinimumButton.raise_()
self.ToUpArea.raise_()
self.ToRightArea.raise_()
self.ToDownArea.raise_()
self.ToLeftArea.raise_()
self.UpRightArea.raise_()
self.DownRightArea.raise_()
self.UpLeftArea.raise_()
self.DownLeftArea.raise_()
def moveEvent(self, event):
pass
def close(self):
super().close()
self.parent_name.close()
def closeEvent(self, event=None):
self.setMinimumSize(0, 0)
self.animation_group = QParallelAnimationGroup(self.parent_name)
self.animation1 = QPropertyAnimation(self.parent_name, b'windowOpacity')
self.animation1.setDuration(100)
self.animation1.setStartValue(0.5)
self.animation1.setEndValue(0)
self.animation1.finished.connect(self.close)
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.__position__ = self.pos()
self.__size__ = self.size()
self.animation0.setEndValue(QRect(self.__position__.x() + 10,
self.__position__.y() + 10,
self.__size__.width() - 20,
self.__size__.height() - 20))
self.animation0.finished.connect(self.close)
self.animation_group.addAnimation(self.animation1)
self.animation_group.addAnimation(self.animation0)
self.animation_group.start()
def showEvent(self, event):
def a01():
pass
def a02():
self.setMinimumSize(self.__startMin)
self.start = True
self.raise_()
if not self.start:
self.setMinimumSize(0, 0)
self.animation_group0 = QParallelAnimationGroup(self.parent_name)
self.animation0 = QPropertyAnimation(self.parent_name, b'windowOpacity')
self.animation0.setDuration(100)
self.animation0.setStartValue(0.4)
self.animation0.setEndValue(1)
# self.animation0.finished.connect(a01)
self.animation_group0.addAnimation(self.animation0)
self.animation1 = QPropertyAnimation(self, b'geometry')
self.animation1.setDuration(100)
self.animation1.setStartValue(QRect(self.normal_geometry.x() + 5,
self.normal_geometry.y() + 5,
self.normal_geometry.width() - 10,
self.normal_geometry.height() - 10))
self.animation1.setEndValue(self.normal_geometry)
self.animation1.finished.connect(a02)
self.animation_group0.addAnimation(self.animation1)
self.animation_group0.start()
else:
def __show():
self.parent_name.showMaximized()
def __show2():
self.setMinimumSize(self.__startMin)
self.setMinimumSize(0, 0)
self.animation_group = QParallelAnimationGroup(self.parent_name)
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(200)
self.animation0.setStartValue(QRect(QGuiApplication.primaryScreen().size().width() // 2 - 400,
QGuiApplication.primaryScreen().size().height(),
100,
62))
self.__position__ = self.pos()
self.__size__ = self.size()
self.animation0.setEndValue(self.__geo__)
self.animation0.finished.connect(__show)
self.animation_group.addAnimation(self.animation0)
self.animation1 = QPropertyAnimation(self.parent_name, b'windowOpacity')
self.animation1.setDuration(200)
self.animation1.setStartValue(0)
self.animation1.setEndValue(1)
self.animation1.finished.connect(__show2)
self.animation_group.addAnimation(self.animation1)
self.animation_group.start()
def __setMainWidget(self):
self.main_widget = QPushButton(self)
self.main_widget.setStyleSheet("QPushButton {"
"border: none;"
f"background-color: rgba({self.background_color_rgba[0]}, {self.background_color_rgba[1]}, {self.background_color_rgba[2]}, 0);"
"}")
def __setNoneEvent(self):
self.ToUpArea.mouseMoveEvent = self.__NoneEvent
self.ToUpArea.mousePressEvent = self.__NoneEvent
self.ToUpArea.mouseReleaseEvent = self.__NoneEvent
self.ToDownArea.mouseMoveEvent = self.__NoneEvent
self.ToDownArea.mousePressEvent = self.__NoneEvent
self.ToDownArea.mouseReleaseEvent = self.__NoneEvent
self.ToLeftArea.mouseMoveEvent = self.__NoneEvent
self.ToLeftArea.mousePressEvent = self.__NoneEvent
self.ToLeftArea.mouseReleaseEvent = self.__NoneEvent
self.ToRightArea.mouseMoveEvent = self.__NoneEvent
self.ToRightArea.mousePressEvent = self.__NoneEvent
self.ToRightArea.mouseReleaseEvent = self.__NoneEvent
self.UpLeftArea.mouseMoveEvent = self.__NoneEvent
self.UpLeftArea.mousePressEvent = self.__NoneEvent
self.UpLeftArea.mouseReleaseEvent = self.__NoneEvent
self.UpRightArea.mouseMoveEvent = self.__NoneEvent
self.UpRightArea.mousePressEvent = self.__NoneEvent
self.UpRightArea.mouseReleaseEvent = self.__NoneEvent
self.DownRightArea.mouseMoveEvent = self.__NoneEvent
self.DownRightArea.mousePressEvent = self.__NoneEvent
self.DownRightArea.mouseReleaseEvent = self.__NoneEvent
self.DownLeftArea.mouseMoveEvent = self.__NoneEvent
self.DownLeftArea.mousePressEvent = self.__NoneEvent
self.DownLeftArea.mouseReleaseEvent = self.__NoneEvent
self.__setWindowStatusMenuOnEdge()
def __setNormalEvent(self):
self.ToUpArea.mouseMoveEvent = self.__toUpMoveEvent
self.ToUpArea.mousePressEvent = self.__toUpPressEvent
self.ToUpArea.mouseReleaseEvent = self.__toUpReleaseEvent
self.ToDownArea.mouseMoveEvent = self.__toDownMoveEvent
self.ToDownArea.mousePressEvent = self.__toDownPressEvent
self.ToDownArea.mouseReleaseEvent = self.__toDownReleaseEvent
self.ToLeftArea.mouseMoveEvent = self.__toLeftMoveEvent
self.ToLeftArea.mousePressEvent = self.__toLeftPressEvent
self.ToLeftArea.mouseReleaseEvent = self.__toLeftReleaseEvent
self.ToRightArea.mouseMoveEvent = self.__toRightMoveEvent
self.ToRightArea.mousePressEvent = self.__toRightPressEvent
self.ToRightArea.mouseReleaseEvent = self.__toRightReleaseEvent
self.MoveableArea.mousePressEvent = self.__movePressEvent
self.MoveableArea.mouseMoveEvent = self.__moveMoveEvent
self.MoveableArea.mouseReleaseEvent = self.__moveReleaseEvent
self.MoveableArea.mouseDoubleClickEvent = self.__titleDoubleClickedEvent
self.UpLeftArea.mouseMoveEvent = self.__UpLeftMoveEvent
self.UpLeftArea.mousePressEvent = self.__UpLeftPressEvent
self.UpLeftArea.mouseReleaseEvent = self.__UpLeftReleaseEvent
self.UpRightArea.mouseMoveEvent = self.__UpRightMoveEvent
self.UpRightArea.mousePressEvent = self.__UpRightPressEvent
self.UpRightArea.mouseReleaseEvent = self.__UpRightReleaseEvent
self.DownRightArea.mouseMoveEvent = self.__DownRightMoveEvent
self.DownRightArea.mousePressEvent = self.__DownRightPressEvent
self.DownRightArea.mouseReleaseEvent = self.__DownRightReleaseEvent
self.DownLeftArea.mouseMoveEvent = self.__DownLeftMoveEvent
self.DownLeftArea.mousePressEvent = self.__DownLeftPressEvent
self.DownLeftArea.mouseReleaseEvent = self.__DownLeftReleaseEvent
self.__setWindowStatusMenu()
def showMaximized(self):
def __max():
self.MaximumButton.setIcon(QIcon("normal.png"))
self.__setNoneEvent()
self.__setWindowStatusMenuOnMax()
self.MoveableArea.mouseDoubleClickEvent = self.__titleDoubleClickedEvent
self.parent_name.will_close()
self.__geo__ = self.geometry()
"""自定义最大化方法"""
self.parent_name.showMaximized()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.__position__ = self.pos()
self.__size__ = self.size()
self.animation0.setEndValue(QRect(0,
0,
self.parent_name.size().width(),
self.parent_name.size().height()))
self.animation0.finished.connect(__max)
self.animation0.start()
def showMinimized(self):
def __min():
self.parent_name.showMin()
self.setGeometry(self.__geo__)
def __min2():
self.parent_name.setWindowOpacity(1)
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation_group = QParallelAnimationGroup(self.parent_name)
self.setMinimumSize(0, 0)
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(200)
self.animation0.setStartValue(self.geometry())
self.__position__ = self.pos()
self.__size__ = self.size()
self.animation0.setEndValue(QRect(QGuiApplication.primaryScreen().size().width() // 2 - 400,
QGuiApplication.primaryScreen().size().height(),
100,
62))
self.animation0.finished.connect(__min)
self.animation_group.addAnimation(self.animation0)
self.animation1 = QPropertyAnimation(self.parent_name, b'windowOpacity')
self.animation1.setDuration(200)
self.animation1.setStartValue(1)
self.animation1.setEndValue(0)
self.animation1.finished.connect(__min2)
self.animation_group.addAnimation(self.animation1)
self.animation_group.start()
def showLined(self):
def __line():
self.__setNoneEvent()
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(
QRect(self.pos().x(), 0, self.size().width(), self.__screen__.height() - self.taskbar_height))
self.animation0.finished.connect(__line)
self.animation0.start()
def showNormal(self):
def __nor():
self.__setNormalEvent()
self.parent_name.will_close()
self.animationprocessing = False
self.setNormalGeometry()
self.MaximumButton.setIcon(QIcon("max.png"))
self.animationprocessing = True
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(self.normal_geometry)
self.animation0.finished.connect(__nor)
self.animation0.start()
self.__geo__ = self.geometry()
def send_close(self):
self.parent_name.will_close()
def setNormalGeometry(self):
if self.is_mouse_pressed == False and self.animationprocessing == False:
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
def getMinMessage(self):
def __min():
self.parent_name.showMinimized()
self.setGeometry(self.__geo__)
def __min2():
self.parent_name.setWindowOpacity(1)
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation_group = QParallelAnimationGroup(self.parent_name)
self.setMinimumSize(0, 0)
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(200)
self.animation0.setStartValue(self.geometry())
self.__position__ = self.pos()
self.__size__ = self.size()
self.animation0.setEndValue(QRect(QGuiApplication.primaryScreen().size().width() // 2 - 400,
QGuiApplication.primaryScreen().size().height(),
100,
62))
self.animation0.finished.connect(__min)
self.animation_group.addAnimation(self.animation0)
self.animation1 = QPropertyAnimation(self.parent_name, b'windowOpacity')
self.animation1.setDuration(200)
self.animation1.setStartValue(1)
self.animation1.setEndValue(0)
self.animation1.finished.connect(__min2)
self.animation_group.addAnimation(self.animation1)
self.animation_group.start()
def showSizeNormal(self):
def __nor():
# self.resize(self.normal_geometry.width(), self.normal_geometry.height())
self.__setNormalEvent()
self.parent_name.will_close()
self.animationprocessing = False
self.setNormalGeometry()
self.MaximumButton.setIcon(QIcon("max.png"))
self.animation0 = QPropertyAnimation(self, b'size')
self.animationprocessing = True
self.animation0.setDuration(50)
self.animation0.setStartValue(QSize(self.geometry().width(), self.geometry().height()))
self.__position__ = self.pos()
self.__size__ = self.size()
self.animation0.setEndValue(QSize(self.normal_geometry.width(), self.normal_geometry.height()))
self.animation0.finished.connect(__nor)
self.animation0.start()
self.__geo__ = self.geometry()
def showUpLeft(self):
def __cor():
self.__setNoneEvent()
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(QRect(0, 0, self.__screen__.width() // 2,
(self.__screen__.height() - self.taskbar_height) // 2))
self.animation0.finished.connect(__cor)
self.animation0.start()
def showFullLeft(self):
def __side():
self.__setNoneEvent()
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(QRect(0, 0, self.__screen__.width() // 2,
self.__screen__.height() - self.taskbar_height))
self.animation0.finished.connect(__side)
self.animation0.start()
def showDownLeft(self):
def __cor():
self.__setNoneEvent()
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(QRect(0, (self.__screen__.height() - self.taskbar_height) // 2,
self.__screen__.width() // 2,
(self.__screen__.height() - self.taskbar_height) // 2))
self.animation0.finished.connect(__cor)
self.animation0.start()
def showUpRight(self):
def __cor():
self.__setNoneEvent()
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(QRect(self.__screen__.width() // 2, 0,
self.__screen__.width() // 2,
(self.__screen__.height() - self.taskbar_height) // 2))
self.animation0.finished.connect(__cor)
self.animation0.start()
def showDownRight(self):
def __cor():
self.__setNoneEvent()
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(QRect(self.__screen__.width() // 2,
(self.__screen__.height() - self.taskbar_height) // 2,
self.__screen__.width() // 2,
(self.__screen__.height() - self.taskbar_height) // 2))
self.animation0.finished.connect(__cor)
self.animation0.start()
def showFullRight(self):
def __side():
self.__setNoneEvent()
self.parent_name.will_close()
self.__geo__ = self.geometry()
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(QRect(self.__screen__.width() // 2, 0,
self.__screen__.width() // 2,
self.__screen__.height() - self.taskbar_height))
self.animation0.finished.connect(__side)
self.animation0.start()
def __showCenter(self):
def __cent():
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
self.animation0 = QPropertyAnimation(self, b'geometry')
self.animation0.setDuration(100)
self.animation0.setStartValue(self.geometry())
self.animation0.setEndValue(QRect(((self.__screen__ - self.size()).width()) // 2,
(self.__screen__.height() - self.size().height() - self.taskbar_height) // 2,
self.size().width(), self.size().height()))
self.animation0.finished.connect(__cent)
self.animation0.start()
def isMaximized(self) -> bool:
"""自定义最大化规则"""
if (self.pos().x(),
self.pos().y(),
self.size().width(),
self.size().height()) == (0, 0, self.parent_name.size().width(), self.parent_name.size().height()):
return True
else:
return False
def __isLined(self) -> bool:
if self.size().height() == self.__screen__.height() - self.taskbar_height and self.pos().y() == 0:
return True
else:
return False
def __isNormal(self):
if not (
self.isMinimized() or self.isMaximized() or self.__isLined() or self.__isFullSide() or self.__isFullCorner()):
return True
else:
return False
def __isFullSide(self) -> bool:
if self.size().width() == self.__screen__.width() // 2 and \
self.size().height() == self.__screen__.height() - self.taskbar_height and \
self.pos().y() == 0:
if self.pos().x() == self.__screen__.width() // 2 or \
self.pos().x() == 0:
return True
else:
return False
else:
return False
def __isFullCorner(self) -> bool:
result = True
if (self.size().width() != self.__screen__.width() // 2 and self.size().height() != (
self.__screen__.height() - self.taskbar_height) // 2) and \
(self.pos().x() <= 5 or self.pos().x() != self.__screen__.width() // 2) and \
(self.pos().y() <= 5 or self.pos().y() != (self.__screen__.height() - self.taskbar_height) // 2):
result = False
return result
def __titleDoubleClickedEvent(self, event: QMouseEvent):
pass
def __setWindowTitleBar(self,
background_color_rgba: tuple[int, int, int, int] | list[int] | set[int],
color_rgba: tuple[int, int, int, int] | list[int] | set[int]):
try:
self.title
except:
self.title = ""
self.WindowTitleBackground = QPushButton(self)
self.WindowTitleBackground.setStyleSheet("QPushButton {"
f"background-color: rgba({background_color_rgba[0]}, {background_color_rgba[1]}, {background_color_rgba[2]}, {background_color_rgba[3]});"
"border: none;"
"}")
self.WindowTitleBackground.setGeometry(0, 0, self.__screen__.width(), self.title_height)
self.WindowTitleBar = QPushButton(self.title, self)
self.WindowTitleBar.setGeometry(self.title_height + 5, 0, self.size().width(), self.title_height)
self.WindowTitleBar.setStyleSheet("QPushButton {"
"text-align: left;"
f'font: normal normal {self.title_font_size}px "微软雅黑";'
f"background-color: rgba(0,0,0,0);"
f"color: rgba({color_rgba[0]}, {color_rgba[1]}, {color_rgba[2]}, {color_rgba[3]});"
"border: none;"
"}")
self.__setMoveableArea()
def __setWindowStatusMenu(self):
self.WindowStatusMenu = QMenu(self)
self.WindowStatusMenu_qss = ("QMenu {"
"background-color:rgb(40, 40, 40);"
"color:rgb(255, 255, 255);"
"border: 1px solid rgb(127, 127, 127);"
"}"
"QMenu:item:selected {"
"background-color:rgb(80, 80, 80);"
"}"
"QMenu:separator{"
"height:1px;"
"background-color:rgba(123,123,123,1);"
"margin-left:22px;"
"margin-right:1px;"
"}"
"QMenu:item:disabled {"
"color:rgb(100,100,100);"
"background-color:rgb(40, 40, 40);"
"}")
self.WindowStatusMenu.setStyleSheet(self.WindowStatusMenu_qss)
self.NormalAction = QAction("还原(&R)", self)
self.NormalAction.setEnabled(False)
self.NormalAction.triggered.connect(self.showNormal)
self.NormalAction.setIcon(QIcon("normalborder.png"))
self.WindowStatusMenu.addAction(self.NormalAction)
self.MoveAction = QAction("移动(&M)", self)
self.WindowStatusMenu.addAction(self.MoveAction)
self.SizeAction = QAction("大小(&S)", self)
self.WindowStatusMenu.addAction(self.SizeAction)
self.MinAction = QAction("最小化(&N)", self)
self.MinAction.setIcon(QIcon("minborder.png"))
self.MinAction.triggered.connect(self.showMinimized)
self.WindowStatusMenu.addAction(self.MinAction)
self.MaxAction = QAction("最大化(&X)", self)
self.MaxAction.triggered.connect(self.showMaximized)
self.MaxAction.setIcon(QIcon("maxborder.png"))
self.WindowStatusMenu.addAction(self.MaxAction)
self.WindowStatusMenu.addSeparator()
self.CloseAction = QAction("关闭(&C)", self)
self.CloseAction.setShortcut("Alt+F4")
self.CloseAction.setIcon(QIcon("closeborder.png"))
self.CloseAction.triggered.connect(self.closeEvent)
self.WindowStatusMenu.addAction(self.CloseAction)
def __setWindowStatusMenuOnEdge(self):
self.WindowStatusMenu = QMenu(self)
self.WindowStatusMenu.setStyleSheet(self.WindowStatusMenu_qss)
self.NormalAction = QAction("还原(&R)", self)
self.NormalAction.triggered.connect(self.showNormal)
self.NormalAction.setIcon(QIcon("normalborder.png"))
self.WindowStatusMenu.addAction(self.NormalAction)
self.MoveAction = QAction("移动(&M)", self)
self.WindowStatusMenu.addAction(self.MoveAction)
self.SizeAction = QAction("大小(&S)", self)
self.SizeAction.setEnabled(False)
self.WindowStatusMenu.addAction(self.SizeAction)
self.MinAction = QAction("最小化(&N)", self)
self.MinAction.setIcon(QIcon("minborder.png"))
self.MinAction.triggered.connect(self.showMinimized)
self.WindowStatusMenu.addAction(self.MinAction)
self.MaxAction = QAction("最大化(&X)", self)
self.MaxAction.triggered.connect(self.showMaximized)
self.MaxAction.setIcon(QIcon("maxborder.png"))
self.WindowStatusMenu.addAction(self.MaxAction)
self.WindowStatusMenu.addSeparator()
self.CloseAction = QAction("关闭(&C)", self)
self.CloseAction.setShortcut("Alt+F4")
self.CloseAction.setIcon(QIcon("closeborder.png"))
self.CloseAction.triggered.connect(self.closeEvent)
self.WindowStatusMenu.addAction(self.CloseAction)
def __setWindowStatusMenuOnMax(self):
self.WindowStatusMenu = QMenu(self)
self.WindowStatusMenu.setStyleSheet(self.WindowStatusMenu_qss)
self.NormalAction = QAction("还原(&R)", self)
self.NormalAction.triggered.connect(self.showNormal)
self.NormalAction.setIcon(QIcon("normalborder.png"))
self.WindowStatusMenu.addAction(self.NormalAction)
self.MoveAction = QAction("移动(&M)", self)
self.WindowStatusMenu.addAction(self.MoveAction)
self.SizeAction = QAction("大小(&S)", self)
self.SizeAction.setEnabled(False)
self.WindowStatusMenu.addAction(self.SizeAction)
self.MinAction = QAction("最小化(&N)", self)
self.MinAction.setIcon(QIcon("minborder.png"))
self.MinAction.triggered.connect(self.showMinimized)
self.WindowStatusMenu.addAction(self.MinAction)
self.MaxAction = QAction("最大化(&X)", self)
self.MaxAction.setEnabled(False)
self.MaxAction.triggered.connect(self.showMaximized)
self.MaxAction.setIcon(QIcon("maxborder.png"))
self.WindowStatusMenu.addAction(self.MaxAction)
self.WindowStatusMenu.addSeparator()
self.CloseAction = QAction("关闭(&C)", self)
self.CloseAction.setShortcut("Alt+F4")
self.CloseAction.setIcon(QIcon("closeborder.png"))
self.CloseAction.triggered.connect(self.closeEvent)
self.WindowStatusMenu.addAction(self.CloseAction)
def __showWindowStatusMenuFromTitle(self, pos: QPoint):
self.parent_name.will_close()
self.WindowStatusMenu.exec(self.MoveableArea.mapToGlobal(pos))
def __showWindowStatusMenuFromIcon(self, pos: QPoint):
self.parent_name.will_close()
self.WindowStatusMenu.exec(self.icon_image.mapToGlobal(pos))
def __setMinimumButton(self):
self.MinimumButton = QPushButton(self)
self.MinimumButton.setIcon(QIcon("min.png"))
self.MinimumButton.setIconSize(QSize(self.title_height - 20, self.title_height - 20))
self.MinimumButton.setStyleSheet("QPushButton {"
"background-color: rgba(25, 25, 25, 0);"
"border: none;"
"}"
"QPushButton:hover {"
f"background-color: rgba({self.button_hoverColor[0]},{self.button_hoverColor[1]},{self.button_hoverColor[2]}, {self.button_hoverColor[3]});"
"}")
self.MinimumButton.resize(self.title_height, self.title_height)
self.MinimumButton.move(self.MaximumButton.pos().x() - self.MinimumButton.size().width(), 0)
self.MinimumButton.clicked.connect(self.showMinimized)
def __setMaximumButton(self):
self.MaximumButton = QPushButton(self)
self.MaximumButton.setIcon(QIcon("max.png"))
self.MaximumButton.setIconSize(QSize(self.title_height - 20, self.title_height - 20))
self.MaximumButton.resize(self.title_height, self.title_height)
self.MaximumButton.setStyleSheet("QPushButton {"
"background-color: rgba(25, 25, 25, 0);"
"color: rgba(255,255,255,1);"
"border: none;"
"}"
"QPushButton:hover {"
f"background-color: rgba({self.button_hoverColor[0]},{self.button_hoverColor[1]},{self.button_hoverColor[2]}, {self.button_hoverColor[3]});"
"color: rgba(255,255,255,1);"
"}")
self.MaximumButton.move(self.CloseButton.pos().x() - self.MaximumButton.size().width(), 0)
self.MaximumButton.clicked.connect(self.MaximumButtonClickedEvent)
def __setCloseButton(self):
self.CloseButton = QPushButton(self)
self.CloseButton.resize(self.title_height, self.title_height)
self.CloseButton.setIcon(QIcon("close.png"))
self.CloseButton.setIconSize(QSize(self.title_height - 20, self.title_height - 20))
self.CloseButton.setStyleSheet("QPushButton {"
"background-color: rgba(25, 25, 25, 0);"
"color: rgba(255,255,255,1);"
"border: none;"
"}"
"QPushButton:hover {"
f"background-color: rgba({self.closeButton_hoverColor[0]},{self.closeButton_hoverColor[1]},{self.closeButton_hoverColor[2]}, {self.closeButton_hoverColor[3]});"
"color: rgba(255,255,255,1);"
"}")
self.CloseButton.move(self.size().width() - self.CloseButton.size().width(), 0)
self.CloseButton.raise_()
self.CloseButton.clicked.connect(self.closeEvent)
def __setThreeButton(self):
self.__setCloseButton()
self.__setMaximumButton()
self.__setMinimumButton()
def MaximumButtonClickedEvent(self):
if self.isMaximized():
self.showNormal()
else:
self.showMaximized()
def __setMoveableArea(self):
self.MoveableArea = QPushButton(self)
self.MoveableArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.MoveableArea.setGeometry(0, 0, self.size().width(), self.title_height)
self.MoveableArea.mousePressEvent = self.__movePressEvent
self.MoveableArea.mouseMoveEvent = self.__moveMoveEvent
self.MoveableArea.mouseReleaseEvent = self.__moveReleaseEvent
self.MoveableArea.mouseDoubleClickEvent = self.__titleDoubleClickedEvent
self.MoveableArea.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.MoveableArea.customContextMenuRequested.connect(self.__showWindowStatusMenuFromTitle)
self.__setToUpArea()
def __setToUpArea(self):
self.ToUpArea = QPushButton(self.MoveableArea)
self.ToUpArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.ToUpArea.setGeometry(0, 0, self.size().width(), self.edge)
self.ToUpArea.setCursor(Qt.CursorShape.SizeVerCursor)
self.ToUpArea.mouseMoveEvent = self.__toUpMoveEvent
self.ToUpArea.mousePressEvent = self.__toUpPressEvent
self.ToUpArea.mouseReleaseEvent = self.__toUpReleaseEvent
self.__setToDownArea()
def __setToDownArea(self):
self.ToDownArea = QPushButton(self)
self.ToDownArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.ToDownArea.setGeometry(0, self.size().height() - self.edge, self.size().width(), self.edge)
self.ToDownArea.setCursor(Qt.CursorShape.SizeVerCursor)
self.ToDownArea.mouseMoveEvent = self.__toDownMoveEvent
self.ToDownArea.mousePressEvent = self.__toDownPressEvent
self.ToDownArea.mouseReleaseEvent = self.__toDownReleaseEvent
self.__setToLeftArea()
def __setToLeftArea(self):
self.ToLeftArea = QPushButton(self)
self.ToLeftArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.ToLeftArea.setGeometry(0, 0, self.edge, self.size().height())
self.ToLeftArea.setCursor(Qt.CursorShape.SizeHorCursor)
self.ToLeftArea.mouseMoveEvent = self.__toLeftMoveEvent
self.ToLeftArea.mousePressEvent = self.__toLeftPressEvent
self.ToLeftArea.mouseReleaseEvent = self.__toLeftReleaseEvent
self.__setToRightArea()
def __setToRightArea(self):
self.ToRightArea = QPushButton(self)
self.ToRightArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.ToRightArea.setGeometry(self.size().width() - self.edge, 0, self.edge, self.size().height())
self.ToRightArea.setCursor(Qt.CursorShape.SizeHorCursor)
self.ToRightArea.mouseMoveEvent = self.__toRightMoveEvent
self.ToRightArea.mousePressEvent = self.__toRightPressEvent
self.ToRightArea.mouseReleaseEvent = self.__toRightReleaseEvent
self.__setUpLeftArea()
def __toRightPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.right_click_position = event.globalPosition().toPoint() - self.pos()
self.beginning_pos_x = QCursor.pos().x()
self.toRightPress = True
self._width_ = self.size().width()
def __toRightMoveEvent(self, event: QMouseEvent):
if self.toRightPress and QCursor.pos().x() >= self.pos().x() + self.minimumWidth() - self.edge:
self.resize(self._width_ + (QCursor.pos().x() - self.beginning_pos_x), self.size().height())
event.accept()
def __toRightReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.toRightPress = False
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
def __movePressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton and \
event.position().y() <= self.title_height:
self.is_mouse_pressed = True
self.mouse_click_position = event.globalPosition().toPoint() - self.pos()
if self.isMaximized():
self.movemode = 1
elif self.__isFullSide():
self.movemode = 3
elif self.__isFullCorner():
self.movemode = 4
elif self.__isLined():
self.movemode = 2
self.__width__ = self.size().width()
else:
self.movemode = 0
event.accept()
def __moveMoveEvent(self, event: QMouseEvent):
if self.is_mouse_pressed:
self.__moveResetEvent()
if self.movemode == 0:
self.move(event.globalPosition().toPoint() - self.mouse_click_position)
elif self.movemode == 3:
self.showSizeNormal()
self.move(
event.globalPosition().toPoint().x() - self.mouse_click_position.x() * self.normal_geometry.width() // (
self.__screen__.width() // 2),
(event.globalPosition().toPoint() - self.mouse_click_position).y())
elif self.movemode == 1:
self.showSizeNormal()
self.move(
event.globalPosition().toPoint().x() - self.mouse_click_position.x() * self.normal_geometry.width() // QGuiApplication.primaryScreen().geometry().width(),
(event.globalPosition().toPoint() - self.mouse_click_position).y())
elif self.movemode == 4:
self.showSizeNormal()
self.move(
event.globalPosition().toPoint().x() - self.mouse_click_position.x() * self.normal_geometry.width() // (
self.__screen__.width() // 2),
(event.globalPosition().toPoint() - self.mouse_click_position).y())
elif self.movemode == 2:
self.showSizeNormal()
self.move(
event.globalPosition().toPoint().x() - self.mouse_click_position.x() * self.normal_geometry.width() // self.__width__,
(event.globalPosition().toPoint() - self.mouse_click_position).y())
if QCursor.pos().y() <= self.screen_border and self.__screen__.width() - self.screen_border > QCursor.pos().x() > self.screen_border:
self.parent_name.will_max()
elif QCursor.pos().x() <= self.screen_border and QCursor.pos().y() <= self.screen_border:
self.parent_name.will_up_left()
elif QCursor.pos().x() >= self.__screen__.width() - self.screen_border and QCursor.pos().y() <= self.screen_border:
self.parent_name.will_up_right()
elif QCursor.pos().x() <= self.screen_border and QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height:
self.parent_name.will_down_left()
elif QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
QCursor.pos().x() >= self.__screen__.width() - self.screen_border:
self.parent_name.will_down_right()
elif QCursor.pos().x() <= self.screen_border <= QCursor.pos().y() <= self.__screen__.height() - self.screen_border:
self.parent_name.will_full_left()
elif QCursor.pos().x() >= self.__screen__.width() - self.screen_border and \
self.__screen__.height() - self.screen_border >= QCursor.pos().y() >= self.screen_border:
self.parent_name.will_full_right()
elif QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and self.screen_border <= QCursor.pos().x() <= self.__screen__.width() - self.screen_border:
self.parent_name.will_center()
else:
self.parent_name.will_close()
event.accept()
def __moveResetEvent(self):
if QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height:
QCursor.setPos(QCursor.pos().x(), self.__screen__.height() - self.taskbar_height)
def __moveReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.is_mouse_pressed = False
if QCursor.pos().y() <= self.screen_border and self.__screen__.width() - self.screen_border > QCursor.pos().x() > self.screen_border:
self.showMaximized()
elif QCursor.pos().x() <= self.screen_border and QCursor.pos().y() <= self.screen_border:
self.showUpLeft()
elif QCursor.pos().x() >= self.__screen__.width() - self.screen_border and QCursor.pos().y() <= self.screen_border:
self.showUpRight()
elif QCursor.pos().x() <= self.screen_border and QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height:
self.showDownLeft()
elif QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
QCursor.pos().x() >= self.__screen__.width() - self.screen_border:
self.showDownRight()
elif QCursor.pos().x() <= self.screen_border <= QCursor.pos().y() <= self.__screen__.height() - self.screen_border:
self.showFullLeft()
elif QCursor.pos().x() >= self.__screen__.width() - self.screen_border and \
self.__screen__.height() - self.screen_border >= QCursor.pos().y() >= self.screen_border:
self.showFullRight()
elif QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and self.screen_border <= QCursor.pos().x() <= self.__screen__.width() - self.screen_border:
self.__showCenter()
elif self.__isNormal():
self.setNormalGeometry()
event.accept()
def __toUpPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton and \
event.position().y() <= self.edge:
self.up_resized = True
self.up_click_position = event.globalPosition().toPoint() - self.pos()
self._x_ = self.pos().x()
self._y_ = self.pos().y()
self._width_ = self.size().width()
self._height_ = self.size().height()
self.beginning_pos_y = QCursor().pos().y()
self.justnow_size_list = []
event.accept()
def __toUpMoveEvent(self, event: QMouseEvent):
if self.up_resized and self.size().height() + self.pos().y() - QCursor.pos().y() >= self.minimumHeight():
self.resize(self._width_, self._height_ - QCursor.pos().y() + self.beginning_pos_y)
self.move(self._x_, (event.globalPosition().toPoint() - self.up_click_position).y())
if QCursor.pos().y() <= 0 and QGuiApplication.primaryScreen().geometry().width() - 5 > QCursor.pos().x() > 5:
self.parent_name.will_lined()
else:
self.parent_name.will_close()
event.accept()
def __toUpReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.up_resized = False
if QCursor.pos().y() <= 0 and QGuiApplication.primaryScreen().geometry().width() - 5 > QCursor.pos().x() > 5:
self.showLined()
else:
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
def __toDownPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.down_click_position = event.globalPosition().toPoint() - self.pos()
self.beginning_pos_y = QCursor.pos().y()
self._height_ = self.size().height()
self.toDownPress = True
def __toDownMoveEvent(self, event: QMouseEvent):
if self.toDownPress and QCursor.pos().y() >= self.minimumHeight() + self.pos().y():
self.resize(self.size().width(), self._height_ + (QCursor.pos().y() - self.beginning_pos_y))
if QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
5 <= QCursor.pos().x() <= self.__screen__.width() - self.edge:
self.parent_name.will_lined()
else:
self.parent_name.will_close()
if self.toDownPress:
self.__moveResetEvent()
event.accept()
def __toDownReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.toDownPress = False
if QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
5 <= QCursor.pos().x() <= self.__screen__.width() - self.edge:
self.showLined()
else:
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
def __toLeftPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton and \
event.position().x() <= self.edge:
self.left_resized = True
self.left_click_position = event.globalPosition().toPoint() - self.pos()
self._x_ = self.pos().x()
self._y_ = self.pos().y()
self._width_ = self.size().width()
self._height_ = self.size().height()
self.beginning_pos_x = QCursor().pos().x()
event.accept()
def __toLeftMoveEvent(self, event: QMouseEvent):
if self.left_resized and self.pos().x() + self.size().width() - QCursor.pos().x() >= self.minimumWidth():
self.resize(self._width_ - QCursor.pos().x() + self.beginning_pos_x, self.height())
self.move((event.globalPosition().toPoint() - self.left_click_position).x(), self._y_)
event.accept()
def __toLeftReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.left_resized = False
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
def __setUpLeftArea(self):
self.UpLeftArea = QPushButton(self)
self.UpLeftArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.UpLeftArea.setGeometry(0, 0, self.edge, self.edge)
self.UpLeftArea.setCursor(Qt.CursorShape.SizeFDiagCursor)
self.UpLeftArea.mouseMoveEvent = self.__UpLeftMoveEvent
self.UpLeftArea.mousePressEvent = self.__UpLeftPressEvent
self.UpLeftArea.mouseReleaseEvent = self.__UpLeftReleaseEvent
self.__setUpRightArea()
def __UpLeftPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.upleft_resized = True
self.upleft_click_position = event.globalPosition().toPoint() - self.pos()
self._x_ = self.pos().x()
self._y_ = self.pos().y()
self._width_ = self.size().width()
self._height_ = self.size().height()
self.beginning_pos = QCursor().pos()
event.accept()
def __UpLeftMoveEvent(self, event: QMouseEvent):
if self.upleft_resized and \
self.pos().x() + self.size().width() - QCursor.pos().x() >= self.minimumWidth():
self.resize(self._width_ - QCursor.pos().x() + self.beginning_pos.x(),
self.size().height())
self.move((event.globalPosition().toPoint() - self.upleft_click_position).x(), self.pos().y())
if self.upleft_resized and \
self.pos().y() + self.size().height() - QCursor.pos().y() >= self.minimumHeight():
self.resize(self.size().width(),
self._height_ - QCursor.pos().y() + self.beginning_pos.y())
self.move(self.pos().x(), (event.globalPosition().toPoint() - self.upleft_click_position).y())
if self.upleft_resized:
if QCursor.pos().y() <= 0 and QGuiApplication.primaryScreen().geometry().width() - 5 > QCursor.pos().x() > 5:
self.parent_name.will_lined()
else:
self.parent_name.will_close()
event.accept()
def __UpLeftReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.parent_name.will_close()
self.upleft_resized = False
if QCursor.pos().y() <= 0 and QGuiApplication.primaryScreen().geometry().width() - 5 > QCursor.pos().x() > 5:
self.showLined()
else:
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
def __setUpRightArea(self):
self.UpRightArea = QPushButton(self)
self.UpRightArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.UpRightArea.setGeometry(self.size().width() - self.edge, 0, self.edge, self.edge)
self.UpRightArea.setCursor(Qt.CursorShape.SizeBDiagCursor)
self.UpRightArea.mouseMoveEvent = self.__UpRightMoveEvent
self.UpRightArea.mousePressEvent = self.__UpRightPressEvent
self.UpRightArea.mouseReleaseEvent = self.__UpRightReleaseEvent
self.__setDownRightArea()
def __UpRightPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.upright_resized = True
self.upleft_click_position = event.globalPosition().toPoint() - self.pos()
self._x_ = self.pos().x()
self._y_ = self.pos().y()
self._width_ = self.size().width()
self._height_ = self.size().height()
self.beginning_pos_right = QCursor().pos()
self.beginning_pos_up = QCursor().pos()
event.accept()
def __UpRightMoveEvent(self, event: QMouseEvent):
if self.upright_resized and QCursor.pos().x() >= self.pos().x() + self.minimumWidth() - self.edge:
self.resize(self._width_ + (QCursor.pos().x() - self.beginning_pos_right.x()), self.size().height())
if self.upright_resized and self.pos().y() + self.size().height() - QCursor.pos().y() >= self.minimumHeight():
self.resize(self.width(), self._height_ - QCursor.pos().y() + self.beginning_pos_up.y())
self.move(self._x_, (event.globalPosition().toPoint() - self.upleft_click_position).y())
if self.upright_resized:
if QCursor.pos().y() <= 0 and QGuiApplication.primaryScreen().geometry().width() - 5 > QCursor.pos().x() > 5:
self.parent_name.will_lined()
else:
self.parent_name.will_close()
event.accept()
def __UpRightReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.parent_name.will_close()
self.upright_resized = False
if QCursor.pos().y() <= 0 and QGuiApplication.primaryScreen().geometry().width() - 5 > QCursor.pos().x() > 5:
self.showLined()
else:
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
def __setDownRightArea(self):
self.DownRightArea = QPushButton(self)
self.DownRightArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.DownRightArea.setGeometry(self.size().width() - self.edge, self.size().height() - self.edge, self.edge,
self.edge)
self.DownRightArea.setCursor(Qt.CursorShape.SizeFDiagCursor)
self.DownRightArea.mouseMoveEvent = self.__DownRightMoveEvent
self.DownRightArea.mousePressEvent = self.__DownRightPressEvent
self.DownRightArea.mouseReleaseEvent = self.__DownRightReleaseEvent
self.__setDownLeftArea()
def __DownRightPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.downright_resized = True
self.upleft_click_position = event.globalPosition().toPoint() - self.pos()
self._x_ = self.pos().x()
self._y_ = self.pos().y()
self._width_ = self.size().width()
self._height_ = self.size().height()
self.beginning_pos_right = QCursor().pos()
self.beginning_pos_down = QCursor().pos()
event.accept()
def __DownRightMoveEvent(self, event: QMouseEvent):
if self.downright_resized and (
event.globalPosition().toPoint() - self.pos()).y() >= self.minimumHeight() - self.edge:
self.resize(self.size().width(), self._height_ + (QCursor.pos().y() - self.beginning_pos_down.y()))
if self.downright_resized and QCursor.pos().x() >= self.pos().x() + self.minimumWidth() - self.edge:
self.resize(self._width_ + (QCursor.pos().x() - self.beginning_pos_right.x()), self.size().height())
if self.downright_resized:
self.__moveResetEvent()
if QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
5 <= QCursor.pos().x() <= self.__screen__.width() - self.edge:
self.parent_name.will_lined()
else:
self.parent_name.will_close()
event.accept()
def __DownRightReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.parent_name.will_close()
self.downright_resized = False
if QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
5 <= QCursor.pos().x() <= self.__screen__.width() - self.edge:
self.showLined()
else:
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
def __setDownLeftArea(self):
self.DownLeftArea = QPushButton(self)
self.DownLeftArea.setStyleSheet("QPushButton {"
"background-color: rgba(255,255,255,0);"
"border: none;"
"}")
self.DownLeftArea.setGeometry(0, self.size().height() - self.edge, self.edge,
self.edge)
self.DownLeftArea.setCursor(Qt.CursorShape.SizeBDiagCursor)
self.DownLeftArea.mouseMoveEvent = self.__DownLeftMoveEvent
self.DownLeftArea.mousePressEvent = self.__DownLeftPressEvent
self.DownLeftArea.mouseReleaseEvent = self.__DownLeftReleaseEvent
def __DownLeftPressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.downleft_resized = True
self.upleft_click_position = event.globalPosition().toPoint() - self.pos()
self._x_ = self.pos().x()
self._y_ = self.pos().y()
self._width_ = self.size().width()
self._height_ = self.size().height()
self.beginning_pos_left = QCursor().pos()
self.beginning_pos_down = QCursor().pos()
event.accept()
def __DownLeftMoveEvent(self, event: QMouseEvent):
if self.downleft_resized and (
event.globalPosition().toPoint() - self.pos()).y() >= self.minimumHeight() - self.edge:
self.resize(self.size().width(), self._height_ + (QCursor.pos().y() - self.beginning_pos_down.y()))
if self.downleft_resized and self.pos().x() + self.size().width() - QCursor.pos().x() >= self.minimumWidth():
self.resize(self._width_ - QCursor.pos().x() + self.beginning_pos_left.x(), self.size().height())
self.move((event.globalPosition().toPoint() - self.upleft_click_position).x(), self._y_)
if self.downleft_resized:
self.__moveResetEvent()
if QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
5 <= QCursor.pos().x() <= self.__screen__.width() - self.edge:
self.parent_name.will_lined()
else:
self.parent_name.will_close()
event.accept()
def __DownLeftReleaseEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton:
self.parent_name.will_close()
self.downleft_resized = False
if QCursor.pos().y() >= self.__screen__.height() - self.taskbar_height and \
5 <= QCursor.pos().x() <= self.__screen__.width() - self.edge:
self.showLined()
else:
self.normal_geometry = QRect(self.pos().x(), self.pos().y(), self.size().width(), self.size().height())
event.accept()
__init__.py
from PySide6.QtCore import QSize
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication
from windowBorder import WindowBorder
from sys import argv
from sys import exit as sys_exit
app = QApplication(argv)
window = WindowBorder(MinimumSize=QSize(100, 70), title="123", icon=QIcon("icon.png"))
sys_exit(app.exec())