-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyButton.py
More file actions
51 lines (38 loc) · 1.48 KB
/
MyButton.py
File metadata and controls
51 lines (38 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from PySide2.QtWidgets import QLabel
from PySide2.QtGui import QPixmap
from PySide2.QtCore import Signal
import os,PySide2,sys
dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
class Mybutton(QLabel):
clicked = Signal()
def __init__(self,*args,parent=None):
super().__init__(parent)
# 1 正常 2 进入 3 按下
self.pic_1 = QPixmap(args[0])
self.pic_2 = QPixmap(args[1])
self.pic_3 = QPixmap(args[2])
# 默认显示正常图片 及pic_1
self.setFixedSize(self.pic_1.size())
self.setPixmap(self.pic_1)
self.enterFlag = False
def enterEvent(self, event:PySide2.QtCore.QEvent):
# 重写鼠标进入事件
# 鼠标移入时,显示第二张图片
self.setPixmap(self.pic_2)
self.enterFlag = True
def leaveEvent(self, event:PySide2.QtCore.QEvent):
# 重写鼠标离开事件
# 鼠标移除时,显示默认图片
self.setPixmap(self.pic_1)
self.enterFlag = False
def mousePressEvent(self, ev:PySide2.QtGui.QMouseEvent):
self.setPixmap(self.pic_3)
def mouseReleaseEvent(self, ev:PySide2.QtGui.QMouseEvent):
if self.enterFlag:
self.setPixmap(self.pic_2)
# 如果鼠标弹起在label上时发射点击信号
self.clicked.emit()
else:
self.setPixmap(self.pic_1)