已然大四蔑歌,想當(dāng)初大一時(shí)在某處聽大神教誨:
大學(xué)四年寫足十萬行代碼隘世,碼路遂成忘苛;前幾天想想是該把欠下的碼量補(bǔ)上了,于是花了一個(gè)晚上寫了個(gè)代碼量記錄器缔莲,其實(shí)也很簡單:用Python寫個(gè)簡陋的展示界面和與數(shù)據(jù)庫交互的按鈕哥纫,用MySQL保存插入的數(shù)據(jù);
-
大概就是這樣子:
1.png
2.png
- 碼量一行填入要提交的代碼量痴奏,語言默認(rèn)是C++蛀骇,備注自己標(biāo)注,點(diǎn)擊commit按鈕則提交读拆, refresh按鈕從數(shù)據(jù)庫讀取數(shù)據(jù)并刷新頁面擅憔;
- 建表語句是:
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;
Python版本是2.7 ,UI是用Python自帶的tkinter庫寫的檐晕,雖然有點(diǎn)難看暑诸,但自己用也還湊合,與MySQL進(jìn)行交互的庫是用mysql-connector-python棉姐,這個(gè)是第三方的屠列,需要自己下載并安裝到環(huán)境里,Google一番很容易伞矩;
用數(shù)據(jù)庫表記錄下插入的記錄笛洛,方便以后做各種統(tǒng)計(jì)和分析
下面直接把代碼貼上來吧:
#!/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', # 數(shù)據(jù)庫用戶名
'password':'xxxxx', # 數(shù)據(jù)庫密碼
'port':3306,
'database':'xxxxx', # 數(shù)據(jù)庫名
'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="語言(默認(rèn):C++): ")
codeTypeEnt = Entry(codeTool, textvariable=codeTypeText)
codeCommentLab = Label(codeTool, text="備注(默認(rèn)為空): ")
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='連接錯(cuò)誤', 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('查詢錯(cuò)誤')
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()
#更新下數(shù)據(jù)
func_codeRefresh()
if __name__ == "__main__":
func_GUI(codeTool)
codeTool.mainloop()
- 代碼很簡單,一看便知乃坤;