2018-10-30 13:37:41 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Created on 2018年10月30日
|
|
|
|
@author: Irony
|
2021-07-13 14:52:26 +08:00
|
|
|
@site: https://pyqt.site , https://github.com/PyQt5
|
2018-10-30 13:37:41 +08:00
|
|
|
@email: 892768447@qq.com
|
2018-12-27 22:58:12 +08:00
|
|
|
@file: ButtomZoom
|
2018-10-30 13:37:41 +08:00
|
|
|
@description:
|
|
|
|
"""
|
2021-07-13 14:52:26 +08:00
|
|
|
try:
|
|
|
|
from PyQt5.QtCore import QPropertyAnimation, QRect
|
|
|
|
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QSpacerItem, QSizePolicy
|
|
|
|
except ImportError:
|
|
|
|
from PySide2.QtCore import QPropertyAnimation, QRect
|
|
|
|
from PySide2.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QSpacerItem, QSizePolicy
|
2018-10-30 13:37:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ZoomButton(QPushButton):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ZoomButton, self).__init__(*args, **kwargs)
|
|
|
|
self._animation = QPropertyAnimation(
|
|
|
|
self, b'geometry', self, duration=200)
|
|
|
|
|
|
|
|
def updatePos(self):
|
|
|
|
# 记录自己的固定的geometry值
|
|
|
|
self._geometry = self.geometry()
|
|
|
|
self._rect = QRect(
|
|
|
|
self._geometry.x() - 6,
|
|
|
|
self._geometry.y() - 2,
|
|
|
|
self._geometry.width() + 12,
|
|
|
|
self._geometry.height() + 4
|
|
|
|
)
|
|
|
|
|
|
|
|
def showEvent(self, event):
|
|
|
|
super(ZoomButton, self).showEvent(event)
|
|
|
|
self.updatePos()
|
|
|
|
|
|
|
|
def enterEvent(self, event):
|
|
|
|
super(ZoomButton, self).enterEvent(event)
|
|
|
|
self._animation.stop() # 停止动画
|
|
|
|
# 修改动画的开始值
|
|
|
|
self._animation.setStartValue(self._geometry)
|
|
|
|
# 修改动画的终止值
|
|
|
|
self._animation.setEndValue(self._rect)
|
|
|
|
self._animation.start()
|
|
|
|
|
|
|
|
def leaveEvent(self, event):
|
|
|
|
super(ZoomButton, self).leaveEvent(event)
|
|
|
|
self._animation.stop() # 停止动画
|
|
|
|
# 修改动画的开始值
|
|
|
|
self._animation.setStartValue(self._rect)
|
|
|
|
# 修改动画的终止值
|
|
|
|
self._animation.setEndValue(self._geometry)
|
|
|
|
self._animation.start()
|
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
|
|
|
self._animation.stop() # 停止动画
|
|
|
|
# 修改动画的开始值
|
|
|
|
self._animation.setStartValue(self._rect)
|
|
|
|
# 修改动画的终止值
|
|
|
|
self._animation.setEndValue(self._geometry)
|
|
|
|
self._animation.start()
|
|
|
|
super(ZoomButton, self).mousePressEvent(event)
|
|
|
|
|
|
|
|
|
2021-07-13 14:52:26 +08:00
|
|
|
class Window(QWidget):
|
2018-10-30 13:37:41 +08:00
|
|
|
# 测试窗口
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-07-13 14:52:26 +08:00
|
|
|
super(Window, self).__init__(*args, **kwargs)
|
2018-10-30 13:37:41 +08:00
|
|
|
# 1. 加入布局中
|
|
|
|
layout = QHBoxLayout(self)
|
|
|
|
layout.addSpacerItem(QSpacerItem(
|
|
|
|
40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
|
|
|
|
self.button1 = ZoomButton('按钮1', self)
|
|
|
|
layout.addWidget(self.button1)
|
|
|
|
layout.addSpacerItem(QSpacerItem(
|
|
|
|
40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
|
|
|
|
# 2. 不在布局中
|
|
|
|
self.button2 = ZoomButton('按钮2', self)
|
|
|
|
|
|
|
|
# 以下两个方法需要重写
|
|
|
|
|
|
|
|
def showEvent(self, event):
|
2021-07-13 14:52:26 +08:00
|
|
|
super(Window, self).showEvent(event)
|
2018-10-30 13:37:41 +08:00
|
|
|
# 更新按钮的位置
|
|
|
|
self.button1.updatePos()
|
2018-10-30 23:59:00 +08:00
|
|
|
# 针对不在控件中的按钮
|
2018-10-30 13:37:41 +08:00
|
|
|
self.button2.move(self.width() - self.button2.width() - 15,
|
2018-10-30 23:59:00 +08:00
|
|
|
self.height() - self.button2.height() - 10)
|
2018-10-30 13:37:41 +08:00
|
|
|
self.button2.updatePos()
|
|
|
|
|
|
|
|
def resizeEvent(self, event):
|
2021-07-13 14:52:26 +08:00
|
|
|
super(Window, self).resizeEvent(event)
|
2018-10-30 13:37:41 +08:00
|
|
|
# 更新按钮的位置
|
|
|
|
self.button1.updatePos()
|
2018-10-30 23:59:00 +08:00
|
|
|
# 针对不在控件中的按钮
|
2018-10-30 13:37:41 +08:00
|
|
|
self.button2.move(self.width() - self.button2.width() - 15,
|
2018-10-30 23:59:00 +08:00
|
|
|
self.height() - self.button2.height() - 10)
|
2018-10-30 13:37:41 +08:00
|
|
|
self.button2.updatePos()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2021-07-13 14:52:26 +08:00
|
|
|
|
2018-10-30 13:37:41 +08:00
|
|
|
app = QApplication(sys.argv)
|
|
|
|
app.setStyleSheet("""QPushButton {
|
|
|
|
border: none;
|
|
|
|
font-weight: bold;
|
|
|
|
font-size: 16px;
|
|
|
|
border-radius: 18px;
|
|
|
|
min-width: 180px;
|
|
|
|
min-height: 40px;
|
|
|
|
background-color: white;
|
|
|
|
}""")
|
2021-07-13 14:52:26 +08:00
|
|
|
w = Window()
|
2018-10-30 13:37:41 +08:00
|
|
|
w.show()
|
|
|
|
sys.exit(app.exec_())
|