[Python] Python日程处理

日程处理

  1. 目标是输入个文本文件关于日程的,就能够到时间的时候显示出来,在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)'''
如果你是一个小型的办公网络,你可以创建一个服务器来进行日程安排,这只是一个开源的小服务器,你果你需要大的免费的软件去http://www.bedework.org/bedework/下载 使用教程 Installation Dependencies Radicale is written in pure python and does not depend on any librabry. It is known to work on Python 2.5, 2.6, 3.0 and 3.1 [1]. Linux users certainly have Python already installed. For Windows and MacOS users, please install Python [2] thanks to the adequate installer. [1] See Python Versions and OS Support for further information. [2] Python download page. Radicale Radicale can be freely downloaded on the project website, download section. Just get the file and unzip it in a folder of your choice. CalDAV Clients At this time Radicale has been tested and works fine with the latests version of Mozilla Sunbird (versions 0.9+), Mozilla Lightning (0.9+), and Evolution (2.30+). More clients will be supported in the future. However, it may work with any calendar client which implements CalDAV specifications too (luck is highly recommanded). To download Sunbird, go to the Sunbird project website and choose the latest version. Follow the instructions depending on your operating system. Simple Usage Starting Server To start Radicale CalDAV server, you have to launch the file called radicale.py located in the root folder of the software package. Using Sunbird or Lightning After starting Sunbird or Lightning, click on File and New Calendar. Upcoming window asks you about your calendar storage. Chose a calendar On the Network, otherwise Sunbird will use its own file system storage instead of Radicale's one and your calendar won't be remotely accessible. Next window asks you to provide information about remote calendar access. Protocol used by Radicale is CalDAV. A standard location for a basic use of a Radicale calendar is http://localhost:5232/user/calendar/, where you can replace user and calendar by some strings of your choice. Calendars are automatically created if needed. You can now customize your calendar by giving it a nickname and a color. This
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值