2018-10-30 13:37:41 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Created on 2018年10月30日
|
|
|
|
@author: Irony
|
|
|
|
@site: http://pyqt5.com https://github.com/892768447
|
|
|
|
@email: 892768447@qq.com
|
2018-12-27 22:58:12 +08:00
|
|
|
@file: ButtomZoom
|
2018-10-30 13:37:41 +08:00
|
|
|
@description:
|
|
|
|
"""
|
|
|
|
from PyQt5.QtCore import QPropertyAnimation, QRect
|
|
|
|
from PyQt5.QtWidgets import QPushButton, QWidget, QHBoxLayout, QSpacerItem,\
|
|
|
|
QSizePolicy
|
|
|
|
|
|
|
|
|
|
|
|
__Author__ = """By: Irony
|
|
|
|
QQ: 892768447
|
|
|
|
Email: 892768447@qq.com"""
|
|
|
|
__Copyright__ = "Copyright (c) 2018 Irony"
|
|
|
|
__Version__ = "Version 1.0"
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
class TestWindow(QWidget):
|
|
|
|
# 测试窗口
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(TestWindow, self).__init__(*args, **kwargs)
|
|
|
|
# 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):
|
|
|
|
super(TestWindow, self).showEvent(event)
|
|
|
|
# 更新按钮的位置
|
|
|
|
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):
|
|
|
|
super(TestWindow, self).resizeEvent(event)
|
|
|
|
# 更新按钮的位置
|
|
|
|
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
|
|
|
|
from PyQt5.QtWidgets import QApplication
|
|
|
|
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;
|
|
|
|
}""")
|
|
|
|
w = TestWindow()
|
|
|
|
w.show()
|
|
|
|
sys.exit(app.exec_())
|