-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
430 lines (356 loc) · 14.9 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
'''
Author: shu-shu-1 [email protected]
Date: 2024-10-26 17:07:33
LastEditors: shu-shu-1 [email protected]
LastEditTime: 2024-11-16 16:49:13
FilePath: \A小树壁纸\7.0\main.py
Description:
Copyright (c) 2024 by shu-shu-1 [email protected], All Rights Reserved.
'''
# ExceedShareServer.exe
from pathlib import Path
import shutil
import subprocess
import threading
import time
from qfluentwidgets import *
from qfluentwidgets import FluentIcon as FIF
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from ui_Next import Ui_Start
import sys
import requests
import os
import io
import json
from PIL import Image
LONG_VER="Core.0.1.0.Alpha.20241116.2-Internal"
UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0"
class Feature:
...
class Widget(QFrame):
def __init__(self, text: str, parent=None):
super().__init__(parent=parent)
# 必须给子界面设置全局唯一的对象名
self.setObjectName(text.replace(' ', '-'))
self.setLayout(QHBoxLayout())
self.layout().addWidget(SubtitleLabel(text))
# 创建按钮并绑定 show_war 方法
self.button = PushButton(text="进行提示测试")
self.button.clicked.connect(self.show_war) # 绑定按钮点击事件
self.layout().addWidget(self.button)
def show_war(self):
InfoBar.info(
title='info测试',
content=f'测试消息',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=-1,
parent=self
)
InfoBar.warning(
title='warning测试',
content=f'测试消息',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=-1,
parent=self
)
InfoBar.error(
title='error测试',
content=f'测试消息',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=-1,
parent=self
)
InfoBar.success(
title='success测试',
content=f'测试消息',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=-1,
parent=self
)
Flyout.create(
icon=InfoBarIcon.SUCCESS,
title='恭喜',
content="消息弹窗测试执行完毕!",
target=self.button,
parent=self,
isClosable=True,
aniType=FlyoutAnimationType.PULL_UP
)
class BingWallpaperThread(QThread):
# 定义信号,用于在下载完成时发送结果
finished = Signal(list)
def __init__(self):
super().__init__()
def run(self):
try:
headers={
'Content-Type':'application/json; charset=utf-8',
'user-agent':UA
}
response = requests.get(
"https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=7&mkt=zh-CN",
headers=headers, #请求头
)
response = json.loads(response.text) #转化为json
imgList = []
for item in response['images']:
imgList.append({
'copyright':item['copyright'], #版权
'date':item['enddate'][0:4]+'-'+item['enddate'][4:6]+'-'+item['enddate'][6:], #时间
'urlbase':'https://cn.bing.com'+item['urlbase'], #原始图片链接
'url':'https://cn.bing.com'+item['url'], #图片链接
'title':item['title'], #标题
'copyrightlink':item['copyrightlink'], #版权链接
})
self.finished.emit(imgList) #返回一个数据数组
except:
self.finished.emit(["Error"])
class MainWindow(QFrame):
def __init__(self):
super().__init__()
# 创建 Feature 实例
self.feature = Feature()
# 使用ui文件导入定义界面类
self.ui = Ui_Start()
self.ui.setupUi(self)
self.setObjectName("home")
class TodayWidget(QFrame):
def __init__(self):
super().__init__()
self.ring = ProgressRing(self)
# 设置进度环取值范围和当前值
self.ring.setRange(0, 110)
self.ring.setValue(0)
self.ring.setGeometry(QRect(50, 50, 30, 30))
self.ring.setFixedSize(30, 30)
self.ring.setStrokeWidth(4)
self.infolabel = QLabel(self)
self.infolabel.setGeometry(QRect(50, 100, 700, 130))
self.infolabel.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 左对齐
self.infolabel.setText("正在从壁纸源获取数据...")
self.infolabel.setWordWrap(True) # 启用自动换行
font1 = QFont()
font1.setFamilies([u"微软雅黑"])
font1.setPointSize(11)
self.infolabel.setFont(font1)
self.today_view = ImageLabel(self)
# 按比例缩放到指定高度
# 圆角
self.today_view.setBorderRadius(8, 8, 8, 8)
self.today_view.setGeometry(QRect(50, 170, 200, 200))
self.download_button = PushButton(text="获取数据", parent=self)
self.download_button.setGeometry(QRect(100, 50, 100, 30))
self.download_button.clicked.connect(self.start_download)
# self
self.setObjectName("today")
def start_download(self):
self.download_thread = BingWallpaperThread()
self.download_thread.finished.connect(self.on_download_finished)
self.ring.setValue(1)
self.download_thread.start()
# Flyout.create(
# icon=InfoBarIcon.SUCCESS,
# title='测试启动成功',
# content="任务已经开始执行",
# target=self.download_button,
# parent=self,
# isClosable=True,
# aniType=FlyoutAnimationType.PULL_UP
# )
def on_download_finished(self, success):
self.ring.setValue(10)
if success and success[0] and "url" in success[0]:
try:
response = requests.get(success[0]["url"], stream=True)
if response.status_code == 200:
total_length = response.headers.get('content-length')
if total_length is None: # 如果没有 Content-Length,就无法显示进度
total_length = 1024*1024
else:
total_length = int(total_length)
bytes_downloaded = 0
buffer = io.BytesIO()
for data in response.iter_content(chunk_size=1024):
bytes_downloaded += len(data)
buffer.write(data)
# 计算并更新进度条
progress = int((bytes_downloaded / total_length) * 100) + 10
self.ring.setValue(progress)
# 使用 Pillow 打开二进制流来判断图片格式
buffer.seek(0)
image = Image.open(buffer)
format = image.format.lower() # 获取图片格式,并转换为小写
# 保存图片到本地
image_path = f"./temp/today.{format}"
with open(image_path, 'wb') as file:
buffer.seek(0)
shutil.copyfileobj(buffer, file)
# 显示图片
pixmap = QPixmap(image_path)
self.today_view.setImage(pixmap)
self.today_view.scaledToHeight(200)
# 完成下载,更新进度条至100%
self.ring.setValue(110)
# 显示成功信息
InfoBar.success(
title='数据获取完成',
content=f'测试执行完成,成功获取数据',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=3000,
parent=self
)
self.infolabel.setText(f"今日标题:{success[0]['title']}\n版权信息:{success[0]['copyright']}")
else:
# 处理请求失败的情况
InfoBar.error(
title='数据获取失败',
content=f'测试执行完成,数据获取失败:状态码 {response.status_code}',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=3000,
parent=self
)
self.infolabel.setText("数据获取失败")
self.ring.setValue(0)
except Exception as e:
# 处理异常
InfoBar.error(
title='数据获取异常',
content=f'测试执行过程中发生异常:{str(e)}',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=3000,
parent=self
)
self.infolabel.setText("数据获取异常")
self.ring.setValue(0)
else:
# 处理 success 数据不正确的情况
InfoBar.error(
title='数据获取完成',
content=f'测试执行完成,数据获取失败',
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=3000,
parent=self
)
self.infolabel.setText("数据获取失败")
self.ring.setValue(0)
class Window(FluentWindow):
""" 主界面 """
def __init__(self):
super().__init__()
self.homeInterface = MainWindow()
self.todayInterface = TodayWidget()
# self.videoInterface = Widget('Video Interface', self)
self.settingInterface = Widget('Setting Interface', self)
self.albumInterface = Widget('Album Interface', self)
self.albumInterface1 = Widget('Album Interface 1', self)
self.favoriteInterface = Widget('Favorite Interface', self)
self.initWindow()
self.splashScreen = SplashScreen(self.windowIcon(), self)
self.splashScreen.setIconSize(QSize(102, 102))
self.show()
self.initNavigation()
# self.splashScreen.finish()
# 设置系统托盘图标
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon("./icon.png")) # 替换为你的图标路径
# 创建托盘图标右键菜单
tray_menu = RoundMenu()
tray_menu.addActions([
Action(FIF.LINK, '显示窗口', triggered=self.showNormal),
Action(FIF.EMBED, '退出', triggered=self.quit_application),
])
# 将右键菜单设置给托盘图标
self.tray_icon.setContextMenu(tray_menu)
# 连接托盘图标激活信号
self.tray_icon.activated.connect(self.on_tray_icon_activated)
# 显示托盘图标
self.tray_icon.show()
#TODO: 重写关闭事件
# self.old_closeEvent = self.closeEvent
# self.closeEvent = self.new_closeEvent
def on_tray_icon_activated(self, reason):
if reason == QSystemTrayIcon.ActivationReason.Trigger:
self.showNormal()
def new_closeEvent(self, event):
# 当用户尝试关闭窗口时,将窗口隐藏并阻止默认关闭行为
if self.tray_icon.isVisible():
self.hide()
event.ignore()
def quit_application(self):
self.tray_icon.hide() # 隐藏托盘图标
QApplication.quit() # 关闭应用程序
def initNavigation(self):
self.addSubInterface(self.homeInterface, FIF.HOME, '主页')
self.addSubInterface(self.todayInterface, FIF.HISTORY, '每日一图')
self.addSubInterface(self.albumInterface, FIF.ALBUM, '壁纸源')
self.addSubInterface(self.albumInterface1, FIF.ALBUM, '二次元源', parent=self.albumInterface)
self.navigationInterface.addSeparator()
self.addSubInterface(self.favoriteInterface, FIF.HEART, '收藏')
self.addSubInterface(self.settingInterface, FIF.SETTING, '设置', NavigationItemPosition.BOTTOM)
InfoBar.warning(
title='警告',
content="早期测试版仅供展示和测试",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.BOTTOM_RIGHT,
duration=-1,
parent=self
)
InfoBar.info(
title='版本号',
content=LONG_VER,
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.BOTTOM_RIGHT,
duration=-1,
parent=self
)
self.splashScreen.finish()
def initWindow(self):
self.resize(900, 700)
self.setWindowIcon(QIcon(':/icon/icon.png'))
self.setWindowTitle(f'小树壁纸 Next {LONG_VER}')
if __name__ == '__main__':
config_path = Path('./config')
# print(os.path.exists("./config/process.json"))
if os.path.exists("./config/process.json") is not True:
try:
config_path.mkdir()
print(f"文件夹 '{config_path}' 创建成功。")
except FileExistsError:
print(f"文件夹 '{config_path}' 已经存在。")
temp_path = Path('./temp')
if os.path.exists("./temp") is not True:
try:
temp_path.mkdir()
print(f"文件夹 '{temp_path}' 创建成功。")
except FileExistsError:
print(f"文件夹 '{temp_path}' 已经存在。")
# with open("./config/process.json", "w+", encoding="utf-8") as f:
# json.dump(Default_process_config, f, ensure_ascii=False, indent=4)
# process_config = Default_process_config
# else:
# with open("./config/process.json", "r", encoding="utf-8") as f:
# process_config = json.load(f)
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec()