-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyWallpaper.py
160 lines (125 loc) · 5.43 KB
/
PyWallpaper.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
import wx
import os
import sys
import wx.adv
import JsonConfig
import SetWallpaper
import NetUtility
import ImageUtility
import TimerWallpaper
def ResourcePath(relativePath):
basePath = getattr(sys,"_MEIPASS",os.path.dirname(os.path.abspath(__file__)))
return os.path.join(basePath,relativePath)
icoFileName = os.path.join(ResourcePath("."), "desktop.ico")
class PyWallpaperApp(wx.App):
def OnInit(self):
jsonConfig = JsonConfig.InitJsonConfig()
self.thisFrame =WallpaperFrame(None,"PyWallpaper")
TimerWallpaper.TimerWallpaper()
return True
def OnExit(self):
self.thisFrame.DestroyTaskBarIcon()
TimerWallpaper.TimerWallpaperStop()
return True
class MyTaskBarIcon(wx.adv.TaskBarIcon):
ID_ABOUT = wx.NewId() # 菜单选项“关于”的ID
ID_EXIT = wx.NewId() # 菜单选项“退出”的ID
ID_SHOW_WEB = wx.NewId() # 菜单选项“显示页面”的ID
ID_TEM_BING = wx.NewId()
ID_TEM_UNSPLASH_RANDOM = wx.NewId()
ID_TEM_UNSPLASH_YHCAO = wx.NewId()
def __init__(self,frame,title):
wx.adv.TaskBarIcon.__init__(self)
self.frame = frame
self.SetIcon(wx.Icon(icoFileName), title) # 设置图标和标题
self.Bind(wx.EVT_MENU, self.onAbout, id=self.ID_ABOUT) # 绑定“关于”选项的点击事件
self.Bind(wx.EVT_MENU, self.onExit, id=self.ID_EXIT) # 绑定“退出”选项的点击事件
self.Bind(wx.EVT_MENU, self.onShowWeb, id=self.ID_SHOW_WEB) # 绑定“显示页面”选项的点击事件
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick)
self.Bind(wx.EVT_MENU, self.onTemBing, id=self.ID_TEM_BING)
self.Bind(wx.EVT_MENU, self.onTemUnsplashRandom, id=self.ID_TEM_UNSPLASH_RANDOM)
self.Bind(wx.EVT_MENU, self.onTemUnsplashYhcao, id=self.ID_TEM_UNSPLASH_YHCAO)
# “关于”选项的事件处理器
def onAbout(self, event):
wx.MessageBox('程序作者:yhcao\n最后更新日期:2019-8-25', "关于")
# “退出”选项的事件处理器
def onExit(self, event):
wx.Exit()
#wx.Colse()
# “显示页面”选项的事件处理器
def onShowWeb(self, event):
if not self.frame.IsShown():
self.frame.Show(True)
self.frame.Raise()
def onTemBing(self,event):
if not self.frame.IsShown():
jsonObj = {"type":"0","timer":"3600","unsplashurl":"https://source.unsplash.com/random/"}
JsonConfig.WriteJsonConfig(jsonObj)
TimerWallpaper.TimerWallpaperRestart()
def onTemUnsplashRandom(self, event):
if not self.frame.IsShown():
jsonObj = {"type":"1","timer":"3600","unsplashurl":"https://source.unsplash.com/random/"}
JsonConfig.WriteJsonConfig(jsonObj)
TimerWallpaper.TimerWallpaperRestart()
def onTemUnsplashYhcao(self, event):
if not self.frame.IsShown():
jsonObj = {"type":"1","timer":"3600","unsplashurl":"https://source.unsplash.com/user/yhcao/likes/"}
JsonConfig.WriteJsonConfig(jsonObj)
TimerWallpaper.TimerWallpaperRestart()
# 创建菜单选项
def CreatePopupMenu(self):
menu = wx.Menu()
menu.Append(self.ID_SHOW_WEB, "显示")
menuTemplate = wx.Menu()
menuTemplate.Append(self.ID_TEM_BING, "bing")
menuTemplate.Append(self.ID_TEM_UNSPLASH_RANDOM, "unsplash-random")
menuTemplate.Append(self.ID_TEM_UNSPLASH_YHCAO, "unsplash-yhcao")
menu.AppendMenu(wx.ID_ANY, "模板",menuTemplate)
menu.Append(self.ID_ABOUT, "关于")
menu.Append(self.ID_EXIT, "退出")
return menu
def OnTaskBarLeftDClick(self, event):
if self.frame.IsIconized():
self.frame.Iconize(False)
if not self.frame.IsShown():
self.frame.Show(True)
self.frame.Raise()
class WallpaperFrame(wx.Frame):
def __init__(self, parent, title):
super(WallpaperFrame, self).__init__(parent, title=title,size=(450, 200))
self.SetIcon(wx.Icon(icoFileName))
self.taskBarIcon = MyTaskBarIcon(self,title) # 显示系统托盘图标
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Bind(wx.EVT_SHOW, self.OnShow)
self.InitUI()
self.Centre()
#self.Show()
def InitUI(self):
self.panel = wx.Panel(self)
self.sizer = wx.GridBagSizer(1, 1)
self.textCtrlSourceJson = wx.TextCtrl(self.panel,style = wx.TE_MULTILINE)
self.sizer.Add(self.textCtrlSourceJson, pos=(0, 0), flag=wx.ALIGN_CENTER | wx.ALL | wx.EXPAND,
border=10)
self.sizer.AddGrowableCol(0)
self.sizer.AddGrowableRow(0)
self.panel.SetSizerAndFit(self.sizer)
def OnShow(self, event):
if self.IsShown():
jsonConfig = JsonConfig.InitJsonConfig()
jsonString = JsonConfig.JsonObjToString(jsonConfig)
self.textCtrlSourceJson.SetValue(jsonString)
else:
jsonString = self.textCtrlSourceJson.GetValue()
jsonObj = JsonConfig.StringToJsonObj(jsonString)
JsonConfig.WriteJsonConfig(jsonObj)
TimerWallpaper.TimerWallpaperRestart()
def OnClose(self, event):
self.Hide()
#self.taskBarIcon.Destroy()
#self.Destroy()
def DestroyTaskBarIcon(self):
self.taskBarIcon.Destroy()
self.Destroy()
if __name__ == "__main__":
app = PyWallpaperApp()
app.MainLoop()