首页投稿(暂停使用,暂停投稿)程序员

Python+mysql编写一个简单的代码量记录器

2017-10-30  本文已影响0人  帅番茄
2.png
create table codeRecord
(
code_id int auto_increment,
code_date date not null default now(),
code_line int not null default 0,
code_type varchar(10) not null default 'C++',
code_comment varchar(20) default " ",
primary key(code_id)
)engine=innodb default charset=utf8;
#!/usr/bin/python
# -*- coding:UTF-8 -*-
from Tkinter import *
import tkFont
import mysql.connector as mariadb

codeTool = Tk()

config = {
'host':'127.0.0.1',  # 如果是跑在本地
'user':'xxxx',  # 数据库用户名
'password':'xxxxx',   # 数据库密码
'port':3306,
'database':'xxxxx',    # 数据库名
'charset':'utf8'
}

feedbackText = StringVar()
feedbackText.set(' ')
codeLineText = StringVar()
codeTypeText = StringVar()
codeCommentText = StringVar()
codeTodayText = StringVar()
codeTotalText = StringVar()

def func_GUI(codeTool):
    codeTool.title("十万行代码")
    
    codeTool.minsize(400, 300)
    # 字体
    labFont = tkFont.Font(family='Fixdsys', size=25, weight=tkFont.BOLD)

    codeLineLab = Label(codeTool, text="码量:  ", font=labFont)
    codeLineEnt = Entry(codeTool, textvariable=codeLineText)
    codeTypeLab = Label(codeTool, text="语言(默认:C++):  ")
    codeTypeEnt = Entry(codeTool, textvariable=codeTypeText)
    codeCommentLab = Label(codeTool, text="备注(默认为空):  ")
    codeCommentEnt = Entry(codeTool, textvariable=codeCommentText)
    codeCommitBtn = Button(codeTool, text='Commit', command=func_codeCommit, width=6)
    codeRefreshBtn = Button(codeTool, text='Refresh', command=func_codeRefresh, width=6)

    showFont = tkFont.Font(size=40, weight=tkFont.BOLD)
    codeTodayLab = Label(codeTool, text="今日代码量:    ", font=labFont)
    codeTodayNum = Label(codeTool, text=" ", textvariable=codeTodayText, font=showFont)
    codeTotalLab = Label(codeTool, text="总代码量:      ", font=labFont)
    codeTotalNum = Label(codeTool, text=" ", textvariable=codeTotalText, font=showFont)

    codeFeedbackLab = Label(codeTool, text='连接错误', textvariable=feedbackText)

    codeLineLab.grid(row=0, column=0)
    codeLineEnt.grid(row=0, column=1)
    codeTypeLab.grid(row=1, column=0)
    codeTypeEnt.grid(row=1, column=1)
    codeCommentLab.grid(row=2, column=0)
    codeCommentEnt.grid(row=2, column=1)

    codeCommitBtn.grid(row=3, column=0)
    codeRefreshBtn.grid(row=3, column=1)

    codeTodayLab.grid(row=4, column=0)
    codeTodayNum.grid(row=4, column=1)

    codeTotalLab.grid(row=5, column=0)
    codeTotalNum.grid(row=5, column=1)

    codeFeedbackLab.place(x=10, y=250)

    return codeTool

def func_codeRefresh():
    try:
        conn = mariadb.connect(**config)
    except Exception , e:
        print e
        feedbackText.set('连接失败')
        conn.close()
        return
    cursor = conn.cursor()

    sqlToday = "select sum(code_line) as code_line from codeRecord where code_date = curdate();"
    sqlTotal = "select sum(code_line) as code_line from codeRecord;"

    try:
        cursor.execute(sqlToday)
        for cl in cursor:
            tVal = cl[0] if cl[0] else "0"
            codeTodayText.set(tVal)
        cursor.execute(sqlTotal)
        for cl in cursor:
            tVal = cl[0] if cl[0] else "0"
            codeTotalText.set(tVal)
    except Exception , e:
        feedbackText.set('查询错误')
        cursor.close()
        conn.close()

    cursor.close()
    conn.close()


def func_codeCommit():
    try:
        conn = mariadb.connect(**config)
    except Exception, e:
        print e
        feedbackText.set('连接失败')
        conn.close()
        return
    cursor = conn.cursor()
    codeNum = codeLineText.get()
    codeType = codeTypeText.get()
    codeComment = codeCommentText.get()
    codeType = "C++" if codeType=="" else codeType
    codeComment = "forFun" if codeComment=="" else codeComment

    sql = "insert into codeRecord(code_line, code_type, code_comment) values(%s, %s, %s);"
    param = (codeNum, codeType, codeComment)
    try:
        cursor.execute(sql, param)
        conn.commit()
    except Exception, e:
        print e
        feedbackText.set('插入失败')
        cursor.close()
        conn.close()
        return
    feedbackText.set('插入成功:'+codeNum)
    cursor.close()
    conn.close()
    #更新下数据
    func_codeRefresh()


if __name__ ==  "__main__":
    func_GUI(codeTool)
    codeTool.mainloop()

上一篇下一篇

猜你喜欢

热点阅读