-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpet.py
142 lines (111 loc) · 3.9 KB
/
pet.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import os
class Pet(object):
def __init__(self, width=1400, height=800):
self.image_url = 'images/sxh/sxh_'
self.image_key = 1
self.image = self.image_url + str(self.image_key) + '.png'
self.rect_x = width
self.rect_y = height
def gif(self):
if self.image_key < 11:
self.image_key += 1
else:
self.image_key = 1
self.image = self.image_url + str(self.image_key) + '.png'
class Label(QLabel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.rightMenu)
def rightMenu(self):
menu = QMenu(self)
menu.addAction(QAction(QIcon('images/eye.png'), '隐藏', self, triggered=self.hide))
menu.addAction(QAction(QIcon('images/exit.png'), '退出', self, triggered=self.quit))
menu.exec_(QCursor.pos())
def quit(self):
self.close()
sys.exit()
def hide(self):
self.setVisible(False)
@staticmethod
def music():
try:
os.startfile(r'E:\Software\CloudMusic\cloudmusic.exe')
except:
print('路径不正确')
@staticmethod
def net():
try:
os.startfile(r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
except:
print('路径不正确')
class App(QWidget):
def __init__(self):
super(App, self).__init__()
self.pet = Pet()
self.is_follow_mouse = False
self.pm_pet = QPixmap(self.pet.image)
self.lb_pet = Label(self)
self.init_ui()
self.tray()
timer = QTimer(self)
timer.timeout.connect(self.gem)
timer.start(250)
def gem(self):
# 宠物实现gif效果
self.pet.gif()
self.pm_pet = QPixmap(self.pet.image)
self.lb_pet.setPixmap(self.pm_pet)
pass
def init_ui(self):
# 窗口大小
screen = QDesktopWidget().screenGeometry()
self.setGeometry(0, 0, screen.width(), screen.height())
# 宠物标签
self.lb_pet.setPixmap(self.pm_pet)
self.lb_pet.move(self.pet.rect_x, self.pet.rect_y)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
self.setAutoFillBackground(False)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.showMaximized()
def mouseDoubleClickEvent(self, QMouseEvent):
self.hide()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.is_follow_mouse = True
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
def mouseMoveEvent(self, event):
if Qt.LeftButton and self.is_follow_mouse:
self.pet.rect_x = QCursor.pos().x() - 77
self.pet.rect_y = QCursor.pos().y() - 63
self.lb_pet.move(self.pet.rect_x, self.pet.rect_y)
event.accept()
def mouseReleaseEvent(self, event):
self.is_follow_mouse = False
self.setCursor(QCursor(Qt.ArrowCursor))
def tray(self):
tray = QSystemTrayIcon(self)
tray.setIcon(QIcon('images/sxh/sxh_0.png'))
display = QAction(QIcon('images/eye.png'), '显示', self, triggered=self.display)
quit = QAction(QIcon('images/exit.png'), '退出', self, triggered=self.quit)
menu = QMenu(self)
menu.addAction(quit)
menu.addAction(display)
tray.setContextMenu(menu)
tray.show()
def quit(self):
self.close()
sys.exit()
def hide(self):
self.lb_pet.setVisible(False)
def display(self):
self.lb_pet.setVisible(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
pet = App()
sys.exit(app.exec_())