pyside2 widget界面去除边框后 添加放大 缩小 关闭按钮并可以鼠标拖动界面移动 界面自由伸缩的代码实现
时间: 2023-09-04 11:10:15 浏览: 82
下面是一个使用PySide2实现无边框窗口、自定义控制按钮、鼠标拖动和界面自由伸缩的示例代码:
```python
from PySide2.QtCore import Qt, QPoint, QSize
from PySide2.QtGui import QColor, QPainter
from PySide2.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel, QVBoxLayout, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
# 设置窗口无边框
self.setWindowFlag(Qt.FramelessWindowHint)
# 设置窗口背景透明
self.setAttribute(Qt.WA_TranslucentBackground)
# 创建控制按钮
self.btn_minimize = QPushButton("—")
self.btn_maximize = QPushButton("□")
self.btn_close = QPushButton("×")
# 设置控制按钮样式
self.btn_minimize.setStyleSheet(
"QPushButton { background-color: rgba(255, 255, 255, 0); color: white; border-radius: 3px; }"
"QPushButton:hover { background-color: #00ccff; color: white; }"
)
self.btn_maximize.setStyleSheet(
"QPushButton { background-color: rgba(255, 255, 255, 0); color: white; border-radius: 3px; }"
"QPushButton:hover { background-color: #00ccff; color: white; }"
)
self.btn_close.setStyleSheet(
"QPushButton { background-color: rgba(255, 255, 255, 0); color: white; border-radius: 3px; }"
"QPushButton:hover { background-color: #ff6666; color: white; }"
)
# 设置控制按钮大小
self.btn_minimize.setFixedSize(20, 20)
self.btn_maximize.setFixedSize(20, 20)
self.btn_close.setFixedSize(20, 20)
# 绑定控制按钮事件
self.btn_minimize.clicked.connect(self.showMinimized)
self.btn_maximize.clicked.connect(self.maximize)
self.btn_close.clicked.connect(self.close)
# 创建顶部布局
self.layout_top = QHBoxLayout()
self.layout_top.addWidget(QLabel("Title"))
self.layout_top.addWidget(self.btn_minimize)
self.layout_top.addWidget(self.btn_maximize)
self.layout_top.addWidget(self.btn_close)
# 创建底部布局
self.layout_bottom = QHBoxLayout()
self.layout_bottom.addStretch()
# 创建左侧布局
self.layout_left = QVBoxLayout()
self.layout_left.addStretch()
# 创建右侧布局
self.layout_right = QVBoxLayout()
self.layout_right.addStretch()
# 创建中心布局
self.layout_center = QHBoxLayout()
self.layout_center.addLayout(self.layout_left)
self.layout_center.addStretch()
self.layout_center.addLayout(self.layout_right)
# 创建主布局
self.layout_main = QVBoxLayout()
self.layout_main.addLayout(self.layout_top)
self.layout_main.addLayout(self.layout_center)
self.layout_main.addLayout(self.layout_bottom)
# 设置主布局
self.setLayout(self.layout_main)
# 设置窗口初始大小
self.resize(800, 600)
def paintEvent(self, e):
# 重绘窗口背景
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(QColor(255, 255, 255, 200))
painter.setPen(Qt.NoPen)
painter.drawRoundedRect(self.rect(), 10, 10)
def mousePressEvent(self, e):
# 鼠标按下事件,记录当前位置
self.offset = e.pos()
def mouseMoveEvent(self, e):
# 鼠标移动事件,计算偏移量并移动窗口
x = e.globalX()
y = e.globalY()
x_w = self.offset.x()
y_w = self.offset.y()
self.move(x - x_w, y - y_w)
def maximize(self):
# 最大化/还原窗口
if self.isMaximized():
self.showNormal()
self.btn_maximize.setText("□")
else:
self.showMaximized()
self.btn_maximize.setText("◻")
def resizeEvent(self, e):
# 窗口大小改变事件,调整布局
margin = 10
self.layout_left.setContentsMargins(margin, margin, margin, self.height() // 2)
self.layout_right.setContentsMargins(margin, margin, margin, self.height() // 2)
if __name__ == "__main__":
app = QApplication([])
window = Window()
window.show()
app.exec_()
```
其中,`setWindowFlag`和`setAttribute`方法用于设置窗口无边框和透明背景。`QPushButton`用于创建控制按钮,并设置样式和大小。`mousePressEvent`和`mouseMoveEvent`方法用于实现鼠标拖动窗口移动。`maximize`方法用于实现最大化/还原窗口。`resizeEvent`方法用于在窗口大小改变时调整布局。