pyside2 widget界面去除边框后 添加放大 缩小 关闭按钮并可以鼠标拖动界面移动 界面自由伸缩的代码实现
时间: 2023-09-04 14:12:12 浏览: 226
pyside2无边框窗口可拖动拉伸边框缩放调节窗口大小
5星 · 资源好评率100%
以下是一个简单的示例代码,展示如何使用 PySide2 实现自由伸缩的界面,同时添加窗口控制按钮和鼠标拖动界面移动的功能。你可以根据自己的需求进行修改和扩展。
```python
from PySide2.QtCore import Qt, QPoint
from PySide2.QtGui import QColor, QCursor
from PySide2.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QFrame, QPushButton
class CustomTitleBar(QWidget):
def __init__(self, parent=None):
super(CustomTitleBar, self).__init__(parent)
self.parent = parent
self.setFixedHeight(30)
self.setWindowTitle(self.parent.windowTitle())
self.titlebar_layout = QHBoxLayout(self)
self.titlebar_layout.setContentsMargins(0, 0, 0, 0)
self.titlebar_layout.setSpacing(0)
self.close_button = QPushButton('×', self)
self.close_button.setFixedSize(30, 30)
self.close_button.setStyleSheet("QPushButton { font-size: 16px; color: white; background-color: red; }"
"QPushButton:hover { background-color: rgb(230, 0, 0); }"
"QPushButton:pressed { background-color: rgb(180, 0, 0); }")
self.close_button.clicked.connect(self.parent.close)
self.titlebar_layout.addWidget(self.close_button)
self.maximize_button = QPushButton('□', self)
self.maximize_button.setFixedSize(30, 30)
self.maximize_button.setStyleSheet("QPushButton { font-size: 16px; color: white; background-color: gray; }"
"QPushButton:hover { background-color: rgb(180, 180, 180); }"
"QPushButton:pressed { background-color: rgb(150, 150, 150); }")
self.maximize_button.clicked.connect(self.toggle_maximize)
self.titlebar_layout.addWidget(self.maximize_button)
self.minimize_button = QPushButton('-', self)
self.minimize_button.setFixedSize(30, 30)
self.minimize_button.setStyleSheet("QPushButton { font-size: 16px; color: white; background-color: gray; }"
"QPushButton:hover { background-color: rgb(180, 180, 180); }"
"QPushButton:pressed { background-color: rgb(150, 150, 150); }")
self.minimize_button.clicked.connect(self.parent.showMinimized)
self.titlebar_layout.addWidget(self.minimize_button)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_pos = event.globalPos() - self.parent.pos()
event.accept()
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.parent.move(event.globalPos() - self.drag_pos)
event.accept()
def toggle_maximize(self):
if self.parent.isMaximized():
self.parent.showNormal()
else:
self.parent.showMaximized()
def update_title(self, title):
self.setWindowTitle(title)
class CustomFrame(QFrame):
def __init__(self, parent=None):
super(CustomFrame, self).__init__(parent)
self.setFrameShape(QFrame.StyledPanel)
self.setFrameShadow(QFrame.Raised)
self.setStyleSheet("background-color: white;")
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_pos = event.globalPos() - self.parent().pos()
event.accept()
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.parent().move(event.globalPos() - self.drag_pos)
event.accept()
class CustomWindow(QMainWindow):
def __init__(self):
super(CustomWindow, self).__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setStyleSheet("background-color: rgba(255, 255, 255, 0.9);")
self.title_bar = CustomTitleBar(self)
self.setCentralWidget(CustomFrame(self))
self.layout().setContentsMargins(0, 0, 0, 0)
self.layout().addWidget(self.title_bar)
def resizeEvent(self, event):
super(CustomWindow, self).resizeEvent(event)
self.title_bar.update_title(self.windowTitle())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton and event.pos().y() <= self.title_bar.height():
self.drag_pos = event.globalPos() - self.pos()
event.accept()
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton and event.pos().y() <= self.title_bar.height():
self.move(event.globalPos() - self.drag_pos)
event.accept()
def enterEvent(self, event):
self.setCursor(QCursor(Qt.ArrowCursor))
def leaveEvent(self, event):
self.setCursor(QCursor(Qt.ArrowCursor))
if __name__ == '__main__':
app = QApplication([])
window = CustomWindow()
window.show()
app.exec_()
```
这段代码实现了以下功能:
- 去除窗口边框,并设置透明背景,实现无边框窗口效果;
- 添加窗口控制按钮(最大化、最小化、关闭);
- 实现鼠标拖动窗口移动的功能;
- 界面自由伸缩。
阅读全文