前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python | “简易清单(EasyBill)增强版”

Python | “简易清单(EasyBill)增强版”

作者头像
LogicPanda
发布2018-08-30 11:43:58
1.1K0
发布2018-08-30 11:43:58
举报

正文共: 5741字 2图

预计阅读时间: 15分钟

图文编辑:逻辑熊猫 图片来源:网络与截图 欢迎朋友圈各种姿势转发*^_^*

软件的关键在于,你想用它做什么!

一、展示

前情提要:关于本软件的前生今世可以查看笔者的历史文章《Python | “一个简单的清单软件easybill”》

二、说明

本软件是EasyBill的增强版,在上一版的基础上增加了管理功能,以清单为单位管理清单(待办事件)和记录(完成记录)。实际上这里有两个文件夹(目录)用来存放清单和记录(上一版中是两个文件)。并增加了自动添加时间标签功能。

使用了三个模块:

os:管理目录(文件夹)和文件

easygui:快速搭建窗口应用

datetime:获取日期和时间

三、涉及到的方法说明

os模块,是一个操作系统接口模块,可以执行与操作系统相关的操作,比如新建目录,切换目录等。这里使用到的方法主要有:

os.mkdir() : 创建目录(文件夹)

os.isdir() : 判断name是否为目录(文件夹)

os.exists() : 判断名字是否存在(可能是文件,也可能是文件夹)

os.chdir():切换目录(文件夹)

有如下代码:

代码语言:javascript
复制
def cd(self, dst = None):
        if dst == self.bil :
            os.chdir(self.affairs)
        if dst == self.rec :
            os.chdir(self.records)

    def exist(self):
        if not op.exists(self.affairs) or not op.isdir(self.affairs) :
            try:
                os.mkdir(self.affairs)
            except:
                pass
        else:
            pass
        if not op.exists(self.records) or not op.isdir(self.records):
            try:
                os.mkdir(self.records)
            except:
                pass
        else:
            pass

用来改变当前目录,以及用来创建文件夹(目录),前面提到,使用两个文件夹来存放清单和记录单,如果这两个文件夹不存在,就创建这两个文件夹,如果存在,就继续程序。这个的except是个保险。exists不会主动去分辨名字是文件还是文件夹。

datetime模块,是一个日期时间管理模块,利用这模块来获取日期和时间,这里涉及到的方法和属性主要有:

datetime.datetime.today():这个方法可以返回当前系统的时间和日期

代码语言:javascript
复制
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2018, 5, 19, 20, 29, 14, 394768)

datetime.datetime.today().weekday():返回当前日期的星期,其中0代表星期日。

这里建立一个列表用来存放对应的字符串。获取日期时间的代码如下:

代码语言:javascript
复制
weekdays = ["Sun", "Mon","Tue","Wed","Thu","Fri","Sat"]
def getdatetime(): # get datetime
    today = datetime.datetime.today()
    year, month, day = today.year, today.month, today.day
    hour, minute, second = today.hour, today.minute, today.second
    weekday = weekdays[today.weekday()]
    return str(year)+"/"+str(month)+"/"+str(day)+' '+weekday+' '+str(hour)+":"+str(minute)

easygui模块,在tkinter基础上开发的建议GUI模块,这里用的方法主要有:

easygui.enterbox():用来获取输入信息

easygui.msgbox() :用来弹出消息框

easygui.choicebox():用来显示选项

easygui.textbox():显示文本内容

四、全部代码

代码语言:javascript
复制
import os
import datetime
import easygui as eg
import os.path as op

weekdays = ["Sun", "Mon","Tue","Wed","Thu","Fri","Sat"]
def getdatetime(): # get datetime
    today = datetime.datetime.today()
    year, month, day = today.year, today.month, today.day
    hour, minute, second = today.hour, today.minute, today.second
    weekday = weekdays[today.weekday()]
    return str(year)+"/"+str(month)+"/"+str(day)+' '+weekday+' '+str(hour)+":"+str(minute)

# main class
class Bill():

    def __init__(self) :
        self.affairs = '待办事件'
        self.records = '完成记录'
        self.bil = '清单'
        self.rec = '记录单'
        self.item = '事件'
        self.recd = '记录'
        self.title = 'EasyBill'

    def cd(self, dst = None):
        if dst == self.bil :
            os.chdir(self.affairs)
        if dst == self.rec :
            os.chdir(self.records)

    def exist(self):
        if not op.exists(self.affairs) or not op.isdir(self.affairs) :
            try:
                os.mkdir(self.affairs)
            except:
                pass
        else:
            pass
        if not op.exists(self.records) or not op.isdir(self.records):
            try:
                os.mkdir(self.records)
            except:
                pass
        else:
            pass
        
    # 创建新清单和创建新记录
    def makebill(self, kind = None):
        
        self.cd(kind)
        
        newbill = eg.enterbox('给新'+ kind + '取个名字吧^_^ ',self.title)
        if not op.exists(newbill):
            f = open(newbill, 'w')
            f.close()
            if op.exists(newbill):
                eg.msgbox('成功新'+ kind + "\"" + newbill+ "\""+'!',"EasyBill")
        else:
            eg.msgbox(kind + "\"" + newbill + "\""+'已存在!',"EasyBill")

        os.chdir('..')
            
    # 修改清单内容或修改记录内容
    def modifybill(self, kind = None):
        self.cd(kind)
        typefile = eg.choicebox('选择要添加内容的'+ kind,'EasyBill', os.listdir('.'))
        newitem = eg.enterbox('请输入新内容:','EasyBill')
        f = open(typefile, 'a')
        newtime = getdatetime()
        f.write(newtime + ' | ' + newitem+'\n')
        f.close()
        eg.msgbox('新内容\"'+newitem+'\"已添加到'+typefile ,"EasyBill")
        os.chdir('..') 
        

    # 显示清单内容或显示记录内容
    def disbill(self, kind = None):

        self.cd(kind)
        
        typefile = eg.choicebox('选择要显示的'+ kind,'EasyBill', os.listdir('.'))
        f = open(typefile, 'r')
        eg.textbox(typefile, 'EasyBill', f)
        f.close
        os.chdir('..')


    # 删除清单内容或删除记录内容
    def rmitem(self, kind):

        self.cd(kind)
        typefile = eg.choicebox('选择要删除内容的'+ kind,'EasyBill', os.listdir('.'))
        f = open(typefile, 'r')
        lines = []
        for each_line in f:
            lines.append(each_line)
        f.close()
        affair = eg.choicebox('请选择要删除的内容', 'EasyBill', lines)
        lines.remove(affair)
        f = open(typefile, 'w')
        for each in lines:
            f.write(each)
        f.close
        os.chdir('..')

    
    # 删除现存清单或删除现存记录
    def rmbill(self, kind = None):
        
        self.cd(kind)

        rmbn = eg.choicebox('选择要删除的'+ kind,'EasyBill', os.listdir('.'))
        os.remove(rmbn)
        eg.msgbox(kind + '\"'+rmbn+ '\"' + '已删除')
        os.chdir('..')

    # 完成事件,即,将待办事件中的内容搬运到完成记录中
    def finishitem(self):
        self.cd(self.bil)
        typefile = eg.choicebox('选择要完成事件的'+ self.bil,'EasyBill', os.listdir('.'))
        f = open(typefile, 'r')
        lines = []
        for each_line in f:
            lines.append(each_line)
        f.close()
        affair = eg.choicebox('请选择要完成的事件', 'EasyBill', lines)
        newitem = affair.split('|')
        lines.remove(affair)
        f = open(typefile, 'w')
        for each in lines:
            f.write(each)
        f.close()
        os.chdir('..')
        self.cd(self.rec)
        typefile = eg.choicebox('选择要记录内容的'+ self.rec,'EasyBill', os.listdir('.'))
        f = open(typefile, 'a')
        newtime = getdatetime()
        f.write(newtime + ' | ' + newitem[1].strip() +'\n')
        f.close()
        eg.msgbox('新内容\"'+newitem[1].strip()+'\"已添加到'+typefile ,"EasyBill")
        os.chdir('..')

    # 最初,功能选择
    def choosefuncs(self):
        allfuncs = ('创建清单','添加事件','显示事件','完成事件','删除事件','删除清单','创建记录单','添加记录','显示记录','删除记录','删除记录单','退出')
        while True:
            getfunc = eg.choicebox('要做什么呢?','EasyBill', allfuncs)
            if getfunc == '创建清单':
                self.makebill(kind = self.bil)
            elif getfunc == '添加事件':
                self.modifybill(kind = self.bil)
            elif getfunc == '显示事件':
                self.disbill(kind = self.bil)
            elif getfunc == '完成事件':
                self.finishitem()
            elif getfunc == '删除事件':
                self.rmitem(kind = self.bil)
            elif getfunc == '删除清单':
                self.rmbill(kind = self.bil)
            elif getfunc == '创建记录单':
                self.makebill(kind = self.rec)
            elif getfunc == '添加记录':
                self.modifybill(kind = self.rec)
            elif getfunc == '显示记录':
                self.disbill(kind = self.rec)
            elif getfunc == '删除记录':
                self.rmitem(kind = self.rec)
            elif getfunc == '删除记录单':
                self.rmbill(kind = self.rec)
            else:
                eg.msgbox('感谢使用EasyBillv1.1 <^_^>',"EasyBill")
                exit()
    

if __name__ == "__main__":
    eg.msgbox('欢迎使用EasyBillv1.1',"EasyBill")
    t = Bill()
    t.exist()
    t.choosefuncs()

五、代码评价

无论从原理上讲还是从实现上讲都很简单,但是实现的很粗糙,依旧存在未解决的bug,关于这部分内容,作者后续会渐渐解决,也换慢慢增加新的功能。

同时也敬请广大读者共同参与。已上传到Github上。

https://github.com/LogicPanda/easybill.git

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-19,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 逻辑熊猫带你玩Python 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com