Python實(shí)現(xiàn)簡(jiǎn)易信息分類存儲(chǔ)軟件
時(shí)間緊任務(wù)重,女神提出的要求有模棱兩可,只能自己考慮各種情況,除了用python還有誰(shuí)能這么短的時(shí)間搞出來(lái)。
程序界面,增刪改查不能少,后悔藥也需要給女神準(zhǔn)備上,由于最后需要打包給女神用,所以選擇了python的自帶庫(kù),tkinter編寫(xiě)界面,我覺(jué)得也不是那么丑,數(shù)據(jù)存儲(chǔ)用sqlite3數(shù)據(jù)庫(kù),可以導(dǎo)出成csv文件,完全用python自帶庫(kù)解決,這樣打包起來(lái)兼容性會(huì)好一點(diǎn)。
查詢界面,可以根據(jù)每個(gè)表的各個(gè)項(xiàng)目分類查詢,如果不輸入查詢關(guān)鍵字,則當(dāng)前類別全部輸出。
匯總信息展示,這里也是程序初始界面。
廢話不多說(shuō),直接上代碼,由于也是業(yè)余時(shí)間搞得,代碼簡(jiǎn)單粗暴,縫縫補(bǔ)補(bǔ),各位大神見(jiàn)笑了。
import tkinter as tk
import sqlite3
import csv
from threading import Thread
import shutil
import os
import time
from tkinter import messagebox
from tkinter import filedialog
from tkinter import ttk
class App(tk.Frame):
def __init__(self,master,*args,**kwargs):
super().__init__(master,*args,**kwargs)
self.dirdict={
"新建":self.new,
"查詢":self.search,
"修改":self.edit,
"刪除":self.delete,
"匯總":self.totale,
"導(dǎo)出":self.export,
"后悔藥":self.regret
}
self.newdict={
"咨詢信息":self.customer_information,
"投標(biāo)信息":self.bidding_information,
"合同信息" :self.contract_information,
"售后信息" :self.service_information,
}
self.newlabelsdict={
"咨詢信息":["日期","公司名稱","聯(lián)系人","聯(lián)系電話","備注"],
"投標(biāo)信息":["招標(biāo)單位","招標(biāo)號(hào)","報(bào)名費(fèi)","保證金","退保證金","開(kāi)票信息",],
"合同信息":["合同號(hào)","簽訂日期","數(shù)量","總價(jià)","客戶名稱","貨期","派工單號(hào)","發(fā)貨地址","回款批次","發(fā)票信息","開(kāi)票信息","合同掃描件"],
"售后信息":["產(chǎn)品型號(hào)","派工號(hào)","貨期","技術(shù)人員","安裝人員","驗(yàn)收","售后1","售后2"],
}
self.prmkey={
"咨詢信息":('company',1),
"投標(biāo)信息":('company',0),
"合同信息":('contract',0),
"售后信息":('jobnum',1),
}
self.new_zh_col={
"咨詢信息":'consulting',
"日期":"date","公司名稱":"company","聯(lián)系人":"contacts","聯(lián)系電話":"telephone","備注":"remarks",
"投標(biāo)信息":'bid',
"招標(biāo)單位":"company","招標(biāo)號(hào)":"number","報(bào)名費(fèi)":"enroll","保證金":"ensure","退保證金":"back","開(kāi)票信息":"invoice",
"合同信息":'contractinfo',
"合同號(hào)":"contract","簽訂日期":"sdate","數(shù)量":"quantity","總價(jià)":"total","客戶名稱":"customer","貨期":"delivery","派工單號(hào)":"oddnum","發(fā)貨地址":"address","回款批次":"batch","發(fā)票信息":"cinfo","開(kāi)票信息":"invoice","合同掃描件":"catpath",
"售后信息":'service',
"產(chǎn)品型號(hào)":"product","派工號(hào)":"jobnum","貨期":"delivery","技術(shù)人員":"artisan","安裝人員":"installer","驗(yàn)收":"check","售后1":"service1","售后2":"service2",
}
self.pack(expand=1,fill="both")
self.con=sqlite3.connect("treasure.db")
self.creat_widget()
def creat_widget(self):
self.frameleft = tk.Frame(self,bg='RoyalBlue',relief="groove",borderwidth=2)
self.frameleft.pack(side='left',expand='no',fill='y',anchor="n")
for i in self.dirdict.keys():
but=tk.Button(self.frameleft,text=i,width="10",)
but.pack(side="top",expand="no",fill="x",anchor="n",padx=4,pady=5,)
but.bind('<Button-1>', self.set_style)
self.frameright = tk.Frame(self,bg='RoyalBlue',relief="groove",borderwidth=2)
self.frameright.pack(side='right',expand='yes',fill='both',padx=3,pady=0)
self.lf2 = tk.LabelFrame(self.frameright,text="信息",relief="groove",borderwidth=1,bg="Wheat")
self.lf2.pack(side="top",expand=1,fill="both",pady=2,)
self.totale()
def set_style(self,event):
for i in self.frameleft.winfo_children():
if isinstance(i,tk.Button):
i.config(fg="black")
event.widget["fg"]="blue"
self.reset(self.frameright)
self.lf1 = tk.Frame(self.frameright,relief="groove",borderwidth=0,bg='RoyalBlue')
self.lf1.pack(side="top",expand=0,fill="x",pady=2,)
self.lf2 = tk.LabelFrame(self.frameright,text="信息",relief="groove",borderwidth=1,bg="Wheat")
self.lf2.pack(side="top",expand=1,fill="both",pady=2,)
self.dirdict.get(event.widget["text"],None)()
self.lf2["text"]=event.widget["text"]
######################################新建內(nèi)容###########################################################################
def new(self):#新建總類
def data_input(event):
self.lf2.config(text=event.widget['text'])
self.reset(self.lf2)
self.newdict.get(event.widget['text'],None)(self.newlabelsdict[event.widget['text']])
for i in self.newdict.keys():
bu=tk.Button(self.lf1,text=i,)
bu.pack(side="left",expand=1,fill="x",padx=5,pady=2,)
bu.bind('<Button-1>', data_input)
def customer_information(self,labellist):#新建客戶信息
this="咨詢信息"
for i in labellist:
if i == labellist[-1]:
tk.Label(self.lf2,text=i,bg="Wheat",width=10).pack(side="top",)
tk.Text(self.lf2,height=6).pack(side="top",expand=1,pady=1,padx=5,fill="both")
else:
e=tk.Entry(self.lf2,)
e.pack(side="top",expand=0,pady=1,padx=5,fill="x")
l=tk.Label(e,text=i,bg="Wheat",width=10)
l.pack(side="right",)
def getdict():
cusdict=self.super_get(labellist,self.lf2)
if self.save_data("INSERT INTO consulting VALUES (?,?,?,?,?)", list(cusdict.values())):
self.super_del(self.lf2)
bu=tk.Button(self.lf2,text="確認(rèn)提交",width=15,command=getdict)
bu.pack(side="bottom",expand=0,padx=5,pady=2,)
def bidding_information(self,labellist):#新建招標(biāo)信息
this="投標(biāo)信息"
for i in labellist:
if i == labellist[-1]:
tk.Label(self.lf2,text=i,bg="Wheat",width=10).pack(side="top",)
tk.Text(self.lf2,height=6).pack(side="top",expand=1,pady=1,padx=5,fill="both")
else:
e=tk.Entry(self.lf2,)
e.pack(side="top",expand=0,pady=1,padx=5,fill="x")
l=tk.Label(e,text=i,bg="Wheat",width=10)
l.pack(side="right",)
def getdict():
cusdict=self.super_get(labellist,self.lf2)
if self.save_data("INSERT INTO bid VALUES (?,?,?,?,?,?)", list(cusdict.values())):
self.super_del(self.lf2)
bu=tk.Button(self.lf2,text="確認(rèn)提交",width=15,command=getdict)
bu.pack(side="bottom",expand=0,padx=5,pady=2,)
def contract_information(self,labellist):#新建合同信息
this="合同信息"
def filenames():
names=filedialog.askopenfilenames(title="上傳合同掃描件")
if names:
filenamesentry.insert(0,",".join(names))
for i in labellist:
if i==labellist[0]:
connum=tk.Entry(self.lf2,)
connum.pack(side="top",expand=0,pady=1,padx=5,fill="x")
tk.Label(connum,text=i,bg="Wheat",width=10).pack(side="right",)
elif i == labellist[-2]:
tk.Label(self.lf2,text=i,bg="Wheat",width=10).pack(side="top",)
tk.Text(self.lf2,height=6).pack(side="top",expand=1,pady=1,padx=5,fill="both")
elif i==labellist[-1]:
filenamesentry=tk.Entry(self.lf2)
filenamesentry.pack(side="top",expand=0,pady=2,padx=5,fill="x")
filebut=tk.Button(filenamesentry,text="點(diǎn)擊上傳合同",height=1,command=filenames)
filebut.pack(side="right",expand=0,padx=0,pady=0,)
else:
e=tk.Entry(self.lf2,)
e.pack(side="top",expand=0,pady=1,padx=5,fill="x")
tk.Label(e,text=i,bg="Wheat",width=10).pack(side="right",)
def getdict():
files=filenamesentry.get()
if files:
number=connum.get() if connum.get() else "無(wú)合同號(hào)"
newcat=os.path.join(os.getcwd(),"mydata\{}{}".format(number,time.strftime(r"-%Y-%m-%d %H-%M-%S",time.localtime()),))
os.mkdir(newcat)
for i in files.split(","):
shutil.move(os.path.join(i),newcat)
filenamesentry.delete(0, "end")
filenamesentry.insert(0,newcat)
cusdict=self.super_get(labellist,self.lf2)
if self.save_data("INSERT INTO contractinfo VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", list(cusdict.values())):
self.super_del(self.lf2)
bu=tk.Button(self.lf2,text="確認(rèn)提交",width=15,command=getdict)
bu.pack(side="bottom",expand=0,padx=5,pady=2,fill="x")
def service_information(self,labellist):#新建售后信息
this="售后信息"
for i in labellist:
e=tk.Entry(self.lf2,)
e.pack(side="top",expand=0,pady=1,padx=5,fill="x")
l=tk.Label(e,text=i,bg="Wheat",width=10)
l.pack(side="right",)
def getdict():
cusdict=self.super_get(labellist,self.lf2)
# check=self.check(self.new_zh_col.get(this,None),self.prmkey.get(this,None)[0],cusdict.get())
if self.save_data("INSERT INTO service VALUES (?,?,?,?,?,?,?,?)", list(cusdict.values())):
self.super_del(self.lf2)
bu=ttk.Button(self.lf2,text="確認(rèn)提交",width=15,command=getdict)
bu.pack(side="bottom",expand=0,padx=5,pady=2,)
#################################################################################################################
def save_data(self,sqldoc,somedata,flag=False):#數(shù)據(jù)庫(kù)存儲(chǔ)存儲(chǔ)客戶信息
cur = self.con.cursor()
try:
cur.execute(sqldoc,somedata)
self.con.commit()
messagebox.showinfo("稟報(bào)女王","女王萬(wàn)歲,您又贏了")
return True
except Exception as e:
messagebox.showwarning("急報(bào)女王","女王我出錯(cuò)了:{}".format(e))
return False
def check(self,this,col,value):
sqldoc="SELECT * FROM {} WHERE {} = '{}' ".format(this,col,value)
cur = self.con.cursor()
cur.execute(sqldoc)
data=cur.fetchone()
return data
def find_data(self,sqldoc):
cur = self.con.cursor()
try:
cur.execute(sqldoc)
datas=cur.fetchall()
if datas:
return datas
else:
messagebox.showwarning("稟報(bào)女王","女王大人,小的什么也沒(méi)搜到")
except Exception as e:
messagebox.showwarning("稟報(bào)女王","女王大人這是一次失誤{}".format(e))
return None
def del_data(self,sqldoc):
cur = self.con.cursor()
cur.execute(sqldoc)
self.con.commit()
messagebox.showinfo("喜報(bào)","女王無(wú)敵,敵人已消滅")
def add_top(self,title):#創(chuàng)建頂級(jí)窗口
top = tk.Toplevel(width=self.master.winfo_screenwidth()//4,height=self.master.winfo_screenheight()//4,)
top.title(title)
return top
def reset(self,widget):#重置該組件,銷毀該組件所有子組件
for i in widget.winfo_children():
i.destroy()
def super_get(self,labellist,wids):#獲取entry和text類得內(nèi)容
cusdict={}
for i ,k in zip(labellist,[i for i in wids.winfo_children() if isinstance(i, (tk.Entry,tk.Text))]):
if isinstance(k,tk.Entry):
cusdict[i] = k.get()
elif isinstance(k, tk.Text):
cusdict[i] = k.get(1.0,'end')
else:
pass
return cusdict
def super_del(self,wids):#刪除entry和text類的內(nèi)容
for wid in wids.winfo_children():
if isinstance(wid,tk.Text):
wid.delete(1.0,"end")
elif isinstance(wid, tk.Entry):
wid.delete(0,"end")
else:
pass
def super_insert(self,wids,text):#為entry或text類組件插入內(nèi)容
ins=[i for i in wids.winfo_children() if isinstance(i, (tk.Entry,tk.Text))]
for wid,value in zip(ins,text):
wid.insert("end",value)
def creat_tree(self,wid,headers,height=4):#建立treeview組件
tree=ttk.Treeview(wid,columns=headers,show='headings',height=height)
for n,i in enumerate(headers):
tree.column(i,width=60,)
tree.heading(column=i,text=i)
sc=ttk.Scrollbar(wid,)
sc['command']=tree.yview
sc.pack(side='right',fill='both')
tree["yscrollcommand"]=sc.set
tree.pack(side="top",fill="both",expand=1)
return tree
def tree_insert(self,table,datas):#插入數(shù)值
# 插入數(shù)據(jù)
if datas:
for index, data in enumerate(datas):
table.insert('', index, values=data)
def tree_del(self,obj):#清除組件內(nèi)內(nèi)容
child=obj.get_children()
for i in child:
obj.delete(i)
def create_lf1_children(self,parent,):#為一下項(xiàng)目提供篩選,搜索選項(xiàng)
def change(event):
cominfo["values"]=self.newlabelsdict[com.get()]
cominfo.current(0)
com=ttk.Combobox(parent,values=tuple(self.newlabelsdict.keys()),state="readonly",)
com.pack(side="left",padx=2,pady=2)
com.current(0)
com.bind('<<ComboboxSelected>>', change)
cominfo=ttk.Combobox(parent,state="readonly",values=self.newlabelsdict[com.get()])
cominfo.pack(side="left",padx=2,pady=2)
cominfo.current(0)
e=ttk.Entry(parent,)
e.pack(side="left",expand=1,pady=1,padx=5,fill="x")
return com,cominfo,e
def start_find(self,arc,colname,e):#便捷函數(shù),為以下項(xiàng)目提供支持
tablename, col = self.new_zh_col.get(arc,None),self.new_zh_col.get(colname,None)
self.lf2.config(text=arc)
headers=self.newlabelsdict.get(arc,None)
table=self.creat_tree(self.lf2,headers)
par=e.get()
sqldoc="SELECT * FROM {} WHERE {} LIKE '%{}%' ".format(tablename,col,par) if par else "SELECT * FROM {} ".format(tablename)
datas=self.find_data(sqldoc)
return table ,datas
def search(self):#查找數(shù)據(jù)
def find():
self.reset(self.lf2)
arc=com.get()
colname = cominfo.get()
table,datas=self.start_find(arc,colname,e)
self.tree_insert(table,datas)
com, cominfo, e= self.create_lf1_children(self.lf1, )
tk.Button(self.lf1,text="開(kāi)始查找",command=find).pack(side="left",expand=0,padx=5,pady=2,)
def edit(self):#編輯數(shù)據(jù)
def tree_selected(event):
name=com.get()
value_e=event.widget.item(event.widget.selection()[0])['values']
self.reset(self.lf2)
self.newdict.get(name,None)(self.newlabelsdict[name])
self.super_insert(self.lf2,value_e)
def find():
self.reset(self.lf2)
arc=com.get()
colname = cominfo.get()
table,datas=self.start_find(arc,colname,e)
self.tree_insert(table,datas)
table.bind("<<TreeviewSelect>>", tree_selected)
com, cominfo, e = self.create_lf1_children(self.lf1, )
tk.Button(self.lf1,text="開(kāi)始查找",command=find).pack(side="left",expand=0,padx=5,pady=2,)
def delete(self):
def tree_selected(event):
name=com.get()
value_e=event.widget.item(event.widget.selection()[0])['values']
flag=messagebox.askokcancel('愛(ài)之深恨之切',"女王大人,確定要放棄它嘛")
if flag:
sqldoc="DELETE FROM {} WHERE {} = '{}' ".format(self.new_zh_col.get(name,None),self.prmkey.get(name,None)[0],value_e[self.prmkey.get(name,None)[1]])
self.del_data(sqldoc)
find()
else:
pass
def find():
self.reset(self.lf2)
arc=com.get()
colname = cominfo.get()
table,datas=self.start_find(arc, cominfo, e)
self.tree_insert(table,datas)
table.bind("<<TreeviewSelect>>", tree_selected)
com, cominfo, e = self.create_lf1_children(self.lf1, )
tk.Button(self.lf1,text="開(kāi)始查找",command=find).pack(side="left",expand=0,padx=5,pady=2,)
def export(self):#導(dǎo)出數(shù)據(jù),存為csv文件
def ex():
cur = self.con.cursor()
file=os.path.join(os.getcwd(),"{}{}.csv".format("數(shù)據(jù)匯總",time.strftime(r"-%Y-%m-%d %H-%M",time.localtime()),))
print(file)
with open(file,"w",newline="") as dd:
wter=csv.writer(dd)
for i in self.newlabelsdict.keys():
sqldoc="SELECT * FROM {} ".format(self.new_zh_col.get(i,None))
cur.execute(sqldoc)
datas=cur.fetchall()
wter.writerow(self.newlabelsdict[i])
wter.writerows(datas)
wter.writerow("")
messagebox.showinfo("喜報(bào)","女王陛下,數(shù)據(jù)已導(dǎo)出完成\n存儲(chǔ)位置{}".format(file))
def beifen():
t = Thread(target=ex)
t.run()
cur = self.con.cursor()
for i, k in self.newlabelsdict.items():
lf21=tk.LabelFrame(self.lf2,text=i,bg="Wheat")
lf21.pack(side="top",fill="both",expand=1,pady=1)
sqldoc="SELECT * FROM {} ".format(self.new_zh_col.get(i,None))
cur.execute(sqldoc)
datas=cur.fetchall()
tk.Label(lf21,text="{}數(shù)據(jù)總數(shù): {}條".format(i,len(datas)),font=("bold",15),bg="Wheat").pack(side="top",expand=1,fill="x")
tk.Button(self.lf2,text="導(dǎo)出數(shù)據(jù)",command=beifen,width=15).pack(side="bottom",expand=0,padx=5,pady=2,)
def totale(self):
cur = self.con.cursor()
for i, k in self.newlabelsdict.items():
lf21=tk.LabelFrame(self.lf2,text=i)
lf21.pack(side="top",fill="both",expand=1,pady=1)
table=self.creat_tree(lf21,k)
sqldoc="SELECT * FROM {} ".format(self.new_zh_col.get(i,None))
cur.execute(sqldoc)
datas=cur.fetchall()
self.tree_insert(table,datas)
def regret(self):
def eat():
self.con.rollback()
messagebox.showinfo("回到從前","女王大人,我們?cè)俅位氐搅藦那?)
tk.Label(self.lf2,text="女王陛下,該吃藥了!!",font=("bold",30,),bg="Wheat").pack(side="top",expand=1,fill="x")
tk.Button(self.lf2,text="立即嗑藥",command=eat,width=15).pack(side="bottom",expand=0,padx=5,pady=2,)
if __name__ == "__main__":
if not os.path.exists("mydata"):
os.mkdir("mydata")
root = tk.Tk()
# root.option_add("*Font", "微軟雅黑")
root.iconbitmap('crown.ico')
root.title("女王的寶庫(kù)")
# root.attributes("-alpha", 0.9)透明度設(shè)置,奈何女神不需要
root.geometry("{}x{}+{}+{}".format(root.winfo_screenwidth()//2,root.winfo_screenheight()//2,root.winfo_screenwidth()//4,root.winfo_screenheight()//4))
app=App(root)
app.mainloop()
到此這篇關(guān)于Python實(shí)現(xiàn)簡(jiǎn)易信息分類存儲(chǔ)軟件的文章就介紹到這了,更多相關(guān)Python信息分類存儲(chǔ)軟件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解設(shè)計(jì)模式中的工廠方法模式在Python程序中的運(yùn)用
這篇文章主要介紹了設(shè)計(jì)模式中的工廠方法模式在Python程序中的運(yùn)用,工廠方法模式主張程序在設(shè)計(jì)時(shí)要可以根據(jù)不同的條件生成各種類的實(shí)例,需要的朋友可以參考下2016-03-03python 使用tkinter與messagebox寫(xiě)界面和彈窗
這篇文章主要介紹了python 使用tkinter與messagebox寫(xiě)界面和彈窗,文章內(nèi)容詳細(xì),具有一的的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03Django+Nginx+uWSGI 定時(shí)任務(wù)的實(shí)現(xiàn)方法
本文主要介紹了Django+Nginx+uWSGI 定時(shí)任務(wù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Python+PyQt5實(shí)現(xiàn)數(shù)據(jù)庫(kù)表格動(dòng)態(tài)增刪改
這篇文章主要為大家介紹如何利用Python中的PyQt5模塊實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)表格的動(dòng)態(tài)增刪改,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03