python实现一个桌面小工具,制作一个桌面的便签提醒工具
参考代码:https://github.com/cosven/memo
参考链接:https://www.jb51.net/article/70821.htm
参考链接:https://blog.csdn.net/v1_vivian/article/details/78418660
核心代码
#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Name=Memo
Comment=Memo Launcher
Exec=/usr/bin/env python "/home/ysw/python/pyqt/memo/widget.py"
Icon=/home/ysw/python/pyqt/memo/img/icon.png
Terminal=false
StartupNotify=true
最近发现了一个非常有意思的python小应用,觉得很赞,所以将学习记录一下。
下载之后大概就是这些文件了,出来的效果也很赞。
小图标制作的也很精致,更加觉得非常有意思了,接下来,看一下源码。
源码
环境是windows+py3
所以,这里我们需要建立一个py2.7的虚拟环境,因为我安装了anaconda,所以可以采用conda建立一个虚拟环境
conda create -n python2.7 python=2.7
激活python2.7的虚拟环境
activate python2.7
退出虚拟环境
deactivate
删除虚拟环境
conda remove -n python2.7 --all
根据这个步骤,我就新建了一个名叫python2.7的虚拟环境。
dataAccess.py
# -*- coding:utf8 -*-
#! /usr/bin/python
'''
dictdata = {
'id': '2012',
'datetime': '10',
'memoList': [
{
'id': '1',
'pid': '2012',
'content': 'hello',
'deadline': '2013',
'finished': True,
},
],
'notfinish': [],
}
'''
import os
import json
'''
assume that filename is date.json
memo id is date
the id of each content in memo is number
'''
def save(dictdata):
'''
1. judge if exists the file
yes: write jsondata
no: new the file and write jsondata
'''
filename = dictdata['id'] + '.json'
f = open(filename, 'w')
jsondata = json.dumps(dictdata)
f.write(jsondata)
f.close
def read(date):
'''
1. if exist today's file
a. judge finish or not and directly load it
2. not exist
a. init the today's file
1. to check log and get lastmemo file
3. finally, return a unfinished content list
'''
contentlist = []
filename = date + '.json'
if os.path.exists(filename):
f = open(filename, 'r')
jsondata = json.load(f)
f.close()
memolist = jsondata['memolist']
for each in memolist:
if not each['finished']:
contentlist.append(each)
else:
f = open('log.json', 'r')
jsondata = json.load(f)
f.close()
lastmemo = jsondata['last']
filename = lastmemo + '.json'
f = open(filename, 'r')
jsondata = json.load(f)
f.close()
memolist = jsondata['memolist']
for each in memolist:
if not each['finished']:
contentlist.append(each)
return contentlist
if __name__ == "__main__":
data = {
'id': '2012',
'datetime': '10',
'memolist': [
{
'id': '1',
'pid': '2012',
'content': 'hello',
'deadline': '2013',
'finished': False,
},
{
'id': '2',
'pid': '2012',
'content': 'world',
'deadline': '2013',
'finished': False,
},
],
'notfinish': [],
}
save(data)
r = read(data['id'])
exe.py
from distutils.core import setup
import py2exe
import sys
#this allows to run it with a simple double click.
sys.argv.append('py2exe')
py2exe_options = {
"includes": ["sip"],
"dll_excludes": ["MSVCP90.dll",],
"compressed": 1,
"optimize": 2,
"ascii": 0,
"bundle_files": 1,
}
window = [{
"script": "hotkey.pyw",
'icon_resources': [(1, 'icon2.ico'),]
}]
setup(
name = 'PyQt Demo',
version = '1.0',
windows = ['hotkey.pyw',],
options = {'py2exe': py2exe_options}
)
myLabel.py
# -*- coding:utf8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class FocusEdit(QWidget):
def __init__(self, parent=None):
super(FocusEdit, self).__init__(parent)
self.initObjects()
self.setObjects()
self.setStyle()
self.setMySizePolicy()
self.connect(self.timeEdit, SIGNAL("dateTimeChanged(QDateTime)"),
self.setDateTime)
def initObjects(self):
self.textEdit = QTextEdit()
self.timeEdit = QDateTimeEdit()
self.layout = QHBoxLayout()
def setObjects(self):
self.layout.addWidget(self.textEdit)
self.layout.addWidget(self.timeEdit)
self.setLayout(self.layout)
def setMySizePolicy(self):
self.layout.setContentsMargins(0,0,0,0)
self.layout.setSpacing(0)
self.timeEdit.setMinimumHeight(40)
self.textEdit.setMinimumHeight(40)
self.textEdit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.timeEdit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
def setStyle(self):
edit = '''
QTextEdit{
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
background-color: #CCCCCC;
selection-color: #CCCCCC;
selection-background-color: #222222;
color: black;
}