当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
0 Star 2 Fork 1

/Clock
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
systray.py 2.66 KB
一键复制 编辑 原始数据 按行查看 历史
提交于 2017-11-20 21:24 . 初始化项目
# -*- coding:utf8 -*-
from os import path, makedirs
from time import strftime, localtime
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication,QSystemTrayIcon,QMenu,QAction
import sys, os
import win32api
def log(msg, type = "info"):
"""
Write information to log file.
type: info, warn, error
"""
log_dir = "./log"
log_file = log_dir + "/clock.log"
if not path.exists(log_dir):
makedirs(log_dir)
curr_time = "[ " + strftime("%Y-%m-%d %H:%M:%S", localtime()) + " " + " ]"
if type == "info":
content = curr_time + " <INFO> " + msg + "\n"
elif type == "warn":
content = curr_time + " <WARNING> " + msg + "\n"
elif type == "error":
content = curr_time + " <ERROR> " + msg + "\n"
else:
content = curr_time + u"没有定义的日志类型!\n"
fp = open(log_file, "a")
fp.write(content)
fp.close()
class SysTray(QSystemTrayIcon):
def __init__(self, parent=None):
super(SysTray, self).__init__(parent)
self.initMenu()
self.setMenu()
self.activated.connect(self.iconClicked)
def initMenu(self):
self.menu = QMenu()
self.setAction = QAction(u"设置", self, triggered = self.showMainDlg)
self.helpAction = QAction(u"帮助", self, triggered=self.showHelp)
self.quitAction = QAction(u"退出", self, triggered=self.exitApp)
self.icon = QIcon('./icon/alarm.png')
def setMenu(self):
self.menu.addAction(self.setAction)
self.menu.addAction(self.helpAction)
self.menu.addAction(self.quitAction)
self.setIcon(self.icon)
self.setContextMenu(self.menu)
def iconClicked(self, reason):
if reason==2 or reason==3:
pw = self.parent()
if pw==None:
return
if pw.isVisible():
pw.hide()
else:
pw.show()
def showMainDlg(self):
pw = self.parent()
if pw==None:
return
if not pw.isVisible():
pw.show()
def exitApp(self):
self.setVisible(False)
sys.exit()
def showHelp(self):
try:
win32api.ShellExecute(0, 'open',
os.getcwd() + '/doc/readme.htm',
'', '', 1)
except:
log(u"找不到文件 " + os.getcwd() + '\\doc\\readme.htm', "error")
if __name__ == "__main__":
app = QApplication(sys.argv)
ti = SysTray()
ti.show()
ti.showMessage(u"提示", u"程序启动", 2)
sys.exit(app.exec_())
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/zlt99/Clock.git
git@gitee.com:zlt99/Clock.git
zlt99
Clock
Clock
master

搜索帮助