日程处理
- 目标是输入个文本文件关于日程的,就能够到时间的时候显示出来,在ubuntu系统上运行。
包括以下几点:
1.了解Tkinter。
1.1 参考文献自己制作Python IDEL
1.1.1 制作最简单的文本框和输出。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import Tkinter as tk
#导入Tkinter模块
#创建一个类
class Application(tk.Frame):
#__init__初始化函数,包含了pack即填充方式。
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
#调用createWidgets()方法
self.createWidgets()
def createWidgets(self):
#定义部件1 hi_there模块,设置为button
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
#定义部件2 QUIT模块,设置为button
self.QUIT = tk.Button(self, text="QUIT", fg="red",
command=root.destroy)
self.QUIT.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
#获取tk实例
root = tk.Tk()
#构造类
app = Application(master=root)
#开始实例化
app.mainloop()
1.1.2 上面的pack是定义填充方式,widget.pack( pack_options ),其中
expand: 当设置为true,部件扩展填充,否则不能使用任何部件的母空间.
fill: 确定是否部件填补分配任何额外的空间,或保持其自己的最小尺寸:无(默认)和X(只填写水平)Y(只填写垂直),或两者兼而有之(填充横向和纵向).
side: 决定哪方父widget包对TOP(默认),下,左,或右.
1.1.3 注意在python2.7中lambda表达式不能直接使用print函数,需要先自定义一个函数然后调用
1.1.4 引入tkFiledialog的时候可以参考文献tkFiledialog引入
from tkFileDialog import *
2.功能模块
2.1 读写CSV文件。
参考CSV操作来写信息到文件中。
2.2 需要定义类方便读写这个CSV文件。
参考类定义,因为需要写入sequence对象,所以在这个类中需要定义类似如下结构:
class stepF:
mID = 0
mTimeStamp = datetime(1990,5,11,8,8,8)
mDuration = 0
mParentTask = 0
mSubTask = 1
mPriority = 0
mItem = "GT,xy"
def getSeq(self):
return [self.mID,self.mTimeStamp,self.mDuration,self.mParentTask,
self.mSubTask,self.mPriority,self.mItem]
最后写CSV如下,定义了那个getSeq函数用来在写入的时候调用:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import csv
from datetime import *
'''csv文件操作'''
csvfile = file('ss.txt','w')
writer = csv.writer(csvfile)
writer.writerow(['ID','TimeStamp','Duration','ParentTask'
,'SubTask','Priority','Item'])
data = []
class stepF:
mID = 0
mTimeStamp = datetime(1990,5,11,8,8,8)
mDuration = 0
mParentTask = 0
mSubTask = 1
mPriority = 0
mItem = "GT,xy"
def getSeq(self):
return [self.mID,self.mTimeStamp,self.mDuration,self.mParentTask,
self.mSubTask,self.mPriority,self.mItem]
s1 = stepF()
s1.ID=1
s2 = stepF()
data.append(s1.getSeq())
data.append(s2.getSeq())
writer.writerows(data)
csvfile.close()
读CSV格式的实例如下,这个init函数是用来在读入的时候构建实例:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import csv
from datetime import *
class stepF:
mID = 0
mTimeStamp = datetime(1990,5,11,8,8,8)
mDuration = 0
mParentTask = 0
mSubTask = 1
mPriority = 0
mItem = "GT,xy"
def __init__(self,all):
self.mID,self.mTimeStamp,self.mDuration,self.mParentTask,self.mSubTask,self.mPriority,self.mItem=all
def getSeq(self):
return [self.mID,self.mTimeStamp,self.mDuration,self.mParentTask,
self.mSubTask,self.mPriority,self.mItem]
csvfile = file('ss.txt','r')
reader = csv.reader(csvfile)
n = 0
data = []
for line in reader:
if n ==0:
n=1
continue
s = stepF(line)
print(s.mItem)
data.append(s)
csvfile.close()
print(data[1].mTimeStamp)
2.3 根据时间对ID排序。可以采用冒泡排序法,如下:
def sortSteps(data):
for i in range(len(data)):
for j in range(len(data)-1,i,-1):
if data[j].mTimeStamp < data[j-1].mTimeStamp:
temp = data[j]
data[j] = data[j-1]
data[j-1]=temp
2.4 线程模块如下,主要是实现了多个task并行执行
def delayrun(delayTime, delayMessage):
root = Tk()
root.title("remind")
root.geometry('500x600')
root.resizable(width=False,height=True)
s = delayMessage
l = Label(root, text=s, bg="green", font=("Arial",24),width=200, height=600)
time.sleep(delayTime)
l.pack()
root.mainloop()
class tkThread(threading.Thread):
def __init__(self, delayTime,delayMessage):
threading.Thread.__init__(self)
self.delayTime =delayTime
self.delayMessage =delayMessage
def run(self):
delayrun(self.delayTime,self.delayMessage)
3.最后把各个模块组合起来实现了读入csv文件并定时显示事件,需要注意的是csv第一行是标题不能占用。ss.txt格式如下:
ID,TimeStamp,Duration,ParentTask,SubTask,Priority,Item
0,2016-06-13 22:00:08,0,0,1,0,"详细规划"
3.1 全部代码如下:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import csv
from datetime import *
from Tkinter import *
import threading
import time
'''定义这个变量用来方便写入读出csv文件'''
class stepF:
mID = 0
mTimeStamp = datetime(1990,5,11,8,8,8)
mDuration = 0
mParentTask = 0
mRepeated = 1
mPriority = 0
mItem = "GT,xy"
def __init__(self,all):
self.mID,self.mTimeStamp,self.mDuration,self.mParentTask,self.mRepeated,self.mPriority,self.mItem=all
def getSeq(self):
return [self.mID,self.mTimeStamp,self.mDuration,self.mParentTask,
self.mRepeated,self.mPriority,self.mItem]
'''对事件变量进行排序,主要是针对时间点来排序'''
def sortSteps(data):
for i in range(len(data)):
for j in range(len(data)-1,i,-1):
if data[j].mTimeStamp < data[j-1].mTimeStamp:
temp = data[j]
data[j] = data[j-1]
data[j-1]=temp
'''输出到文件中去'''
def outputData(data):
csvfile = file('ss.txt','w')
writer = csv.writer(csvfile)
writer.writerow(['ID','TimeStamp','Duration','ParentTask'
,'SubTask','Priority','Item'])
for i in range(len(data)):
data[i].mID = i
writer.writerow(data[i].getSeq())
csvfile.close()
''' 读入数据'''
def inputData(data):
csvfile = file('ss.txt','r')
reader = csv.reader(csvfile)
n = 0
for line in reader:
if n ==0:
n=1
continue
s = stepF(line)
data.append(s)
csvfile.close()
''' 在屏幕上显示提示信息'''
def delayrun(delayTime, delayMessage):
root = Tk()
root.title("remind")
root.geometry('500x600')
root.resizable(width=False,height=True)
s = delayMessage
l = Label(root, text=s, bg="green", font=("Arial",24),width=200, height=600)
time.sleep(delayTime)
l.pack()
root.mainloop()
''' thread用来显示多个事件'''
class tkThread(threading.Thread):
def __init__(self, delayTime,delayMessage):
threading.Thread.__init__(self)
self.delayTime =delayTime
self.delayMessage =delayMessage
def run(self):
delayrun(self.delayTime,self.delayMessage)
''' 用来存储读入的事件'''
data = []
inputData(data)
sortSteps(data)
''' 运行多个线程,当时间到了以后显示tk信息'''
for i in range(len(data)):
dt = datetime.today()
nt = datetime.strptime(data[i].mTimeStamp,"%Y-%m-%d %H:%M:%S")
if dt> nt:
continue
print(str(i))
print(str((nt-dt).total_seconds()))
print(data[i].mItem)
t=tkThread((nt-dt).total_seconds(),data[i].mItem)
t.start()
print("wait or not")
'''outputData(data)'''