建立releaseclass.py
# -*- coding: utf-8 -*- import sys import os import sqlite3 import tkinter.ttk from tkinter import * from tkinter.messagebox import * import datetime import baseclass import framebaseclass import constant class release(framebaseclass.baseFrame): def __init__(self,master,user,framename): self.top=master self.user=user self.framename=framename super( ).__init__(self.top,self.user,self.framename) columns1 = ("量具编码","量具描述","状态","部门","使用人","位置") self.tree1=ttk.Treeview(self.top, show="headings", columns=columns1) self.tree1.column("量具编码",width=60,anchor='center') self.tree1.column("量具描述",width=60,anchor='center') self.tree1.column("状态",width=60,anchor='center') self.tree1.column("部门",width=60,anchor='center') self.tree1.column("使用人",width=60,anchor='center') self.tree1.column("位置",width=60,anchor='center') self.tree1.heading("量具编码",text="量具编码") self.tree1.heading("量具描述",text="量具描述") self.tree1.heading("状态",text="状态") self.tree1.heading("部门",text="部门") self.tree1.heading("使用人",text="使用人") self.tree1.heading("位置",text="位置") self.tree1.place(relx=0.01,rely=0.1,relwidth=0.95,relheight=0.6) label_ID=Label(self.top,text="输入量具编码:",justify=RIGHT) label_ID.place(relx=0.01,rely=0.02,relwidth=0.1,height=20) self.var_ID=StringVar() self.entry_ID=Entry(self.top,textvariable=self.var_ID) self.entry_ID.place(relx=0.11,rely=0.02,relwidth=0.1,height=20) self.var_ID.set(self.get_first_from_db()) buttonquery=Button(self.top,text="查询:",command=self.query_by_code) buttonquery.place(relx=0.25,rely=0.02,relwidth=0.1,height=20) buttondelete=Button(self.top,text="选择并删除",command=self.delete_tree_conent) buttondelete.place(relx=0.4,rely=0.02,relwidth=0.1,height=20) buttonsave=Button(self.top,text="保存:",command=self.update_database) buttonsave.place(relx=0.65,rely=0.02,relwidth=0.1,height=20) self.tree1.bind('<Double-1>', self.choosetree1) def get_data_by_code(self): code=self.entry_ID.get() self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Code量具编码,Description描述,Status状态,Department使用部门,Used_By使用人,Location库位 FROM instruments_info WHERE Code量具编码='%s' " % (code,) #print(sql) c.execute(sql) li=c.fetchall() self.conn.commit() self.conn.close() return li def get_user(self): code=self.entry_ID.get() self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Username FROM user_info WHERE isActive='Y' and error<3" c.execute(sql) li=c.fetchall() self.conn.commit() self.conn.close() return li def query_by_code(self): content=self.get_data_by_code() for item in content: self.tree1.insert("" ,"end",values=(item[0],item[1],item[2],item[3],item[4],item[5])) def delete_tree_conent(self): x=self.tree1.get_children() line=self.tree1.selection() if x: self.tree1.delete(line) else: showwarning('警告', "没有数据可以删除!") def choosetree1(self,event): for self.item in self.tree1.selection(): print("self.tree1.selection()",self.tree1.selection()) item_text =self.tree1.item(self.item, "values") print("item",item_text) self.row = self.tree1.identify_row(event.y) print("row",self.row,event.y) self.column=self.tree1.identify_column(event.x) print("column",self.column,event.x) cn = int(str(self.column).replace('#',''),16) rn = int(str(self.row).replace('I',''),16) if self.column=='#3': self.status=constant.STATUS self.entryedit =tkinter.ttk.Combobox(self.top,value=tuple(self.status)) self.entryedit.place(x=100+(cn-1)*150, y=100+rn*20) self.okb = ttk.Button(self.top, text='OK', width=3, command=self.saveedit) self.okb.place(x=120+(cn-1)*150, y=130+rn*20) if self.column=='#4': self.dept=constant.DEPT self.entryedit =tkinter.ttk.Combobox(self.top,value=tuple(self.dept)) self.entryedit.place(x=100+(cn-1)*150, y=100+rn*20) self.okb = ttk.Button(self.top, text='OK', width=3, command=self.saveedit) self.okb.place(x=120+(cn-1)*150, y=130+rn*20) if self.column=='#5': self.user=self.get_user() self.entryedit =tkinter.ttk.Combobox(self.top,value=tuple(self.user)) self.entryedit.place(x=100+(cn-1)*150, y=100+rn*20,width=150,height =20) self.okb = ttk.Button(self.top, text='OK', width=3, command=self.saveedit) self.okb.place(x=120+(cn-1)*150, y=130+rn*20) if self.column=='#6': self.location=constant.LOCATION self.entryedit =tkinter.ttk.Combobox(self.top,value=tuple(self.location)) self.entryedit.place(x=100+(cn-1)*150, y=100+rn*20) self.okb = ttk.Button(self.top, text='OK', width=3, command=self.saveedit) self.okb.place(x=120+(cn-1)*150, y=130+rn*20) def saveedit(self): self.tree1.set(self.item, column=self.column, value=self.entryedit.get()) self.entryedit.destroy() self.okb.destroy() def update_database(self): count=0 self.conn=sqlite3.connect("database.db") c=self.conn.cursor() for item in self.tree1.get_children(): item_text = self.tree1.item(item,"values") code=item_text[0] state=item_text[2] dept=item_text[3] user=item_text[4] location=item_text[5] sql="UPDATE instruments_info SET Department使用部门='%s',Used_By使用人='%s',Location库位='%s' ,Status状态='%s' WHERE Code量具编码='%s' " % (dept,user,location,state,code) print(sql) c.execute(sql) count+=1 self.conn.commit() self.conn.close() self.top.withdraw() showinfo('提示', "共保存"+str(count)+"条记录") self.top.deiconify() def get_first_from_db(self): self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Code量具编码 FROM instruments_info order by ID ASC LIMIT 1" c.execute(sql) li=c.fetchone()[0] self.conn.commit() self.conn.close() return li def get_status_from_db(self,code): self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Status状态 FROM instruments_info WHERE Code量具编码='%s'"%(code) c.execute(sql) li=c.fetchone()[0] self.conn.commit() self.conn.close() return li if __name__ =="__main__": root = Tk() release(root,"usertest","frame") root.mainloop()建立scrapclass.py
# -*- coding: utf-8 -*- import sys import os import sqlite3 import tkinter.ttk from tkinter import * from tkinter.messagebox import * import datetime import baseclass import framebaseclass import constant class scrap(framebaseclass.baseFrame): def __init__(self,master,user,framename): self.top=master self.user=user self.framename=framename super( ).__init__(self.top,self.user,self.framename) columns1 = ("量具编码","量具描述","型号","状态","报废方式","是否从台账移除") self.tree1=ttk.Treeview(self.top, show="headings", columns=columns1) self.tree1.column("量具编码",width=60,anchor='center') self.tree1.column("量具描述",width=160,anchor='center') self.tree1.column("型号",width=60,anchor='center') self.tree1.column("状态",width=60,anchor='center') self.tree1.column("报废方式",width=60,anchor='center') self.tree1.column("是否从台账移除",width=160,anchor='center') self.tree1.heading("量具编码",text="量具编码") self.tree1.heading("量具描述",text="量具描述") self.tree1.heading("型号",text="型号") self.tree1.heading("状态",text="状态") self.tree1.heading("报废方式",text="报废方式",) self.tree1.heading("是否从台账移除",text="是否从台账移除") self.tree1.place(relx=0.01,rely=0.1,relwidth=0.95,relheight=0.6) label_ID=Label(self.top,text="输入量具编码:",justify=RIGHT) label_ID.place(relx=0.01,rely=0.02,relwidth=0.1,height=20) self.var_ID=StringVar() self.entry_ID=Entry(self.top,textvariable=self.var_ID) self.entry_ID.place(relx=0.11,rely=0.02,relwidth=0.1,height=20) self.var_ID.set(self.get_first_from_db()) self.buttonquery=Button(self.top,text="查询:",command=self.query_by_code) self.buttonquery.place(relx=0.25,rely=0.02,relwidth=0.1,height=20) self.buttondelete=Button(self.top,text="选择并删除",command=self.delete_tree_conent) self.buttondelete.place(relx=0.4,rely=0.02,relwidth=0.1,height=20) self.buttondelete=Button(self.top,text="保存",command=self.update_database) self.buttondelete.place(relx=0.8,rely=0.02,relwidth=0.1,height=20) self.tree1.bind('<Double-1>', self.choosetree1) def get_data_by_code(self): code=self.entry_ID.get() self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Code量具编码,Description描述,Type型号,Status状态 FROM instruments_info WHERE Code量具编码='%s' " % (code,) c.execute(sql) li=c.fetchall() self.conn.commit() self.conn.close() return li def query_by_code(self): content=self.get_data_by_code() for item in content: self.tree1.insert("" ,"end",values=(item[0],item[1],item[2],item[3],"", "")) def delete_tree_conent(self): x=self.tree1.get_children() line=self.tree1.selection() if x: self.tree1.delete(line) else: showwarning('警告', "没有数据可以删除!") def choosetree1(self,event): for self.item in self.tree1.selection(): item_text =self.tree1.item(self.item, "values") self.row = self.tree1.identify_row(event.y) self.column=self.tree1.identify_column(event.x) cn = int(str(self.column).replace('#',''),16) rn = int(str(self.row).replace('I',''),16) if self.column=='#5': self.disposal=["直接报废","资源回收","其它"] self.entryedit =tkinter.ttk.Combobox(self.top,value=tuple(self.disposal)) self.entryedit.place(x=160+(cn-1)*150, y=100+rn*20) self.okb = ttk.Button(self.top, text='OK', width=3, command=self.saveedit) self.okb.place(x=160+(cn-1)*150, y=130+rn*20) if self.column=='#6': self.option=["Y","N"] self.entryedit =tkinter.ttk.Combobox(self.top,value=tuple(self.option)) self.entryedit.place(x=160+(cn-1)*150, y=100+rn*20,width=150,height =20) self.okb = ttk.Button(self.top, text='OK', width=3, command=self.saveedit) self.okb.place(x=160+(cn-1)*150, y=130+rn*20) def saveedit(self): self.tree1.set(self.item, column=self.column, value=self.entryedit.get()) self.entryedit.destroy() self.okb.destroy() def update_database(self): count=0 self.conn=sqlite3.connect("database.db") c=self.conn.cursor() for item in self.tree1.get_children(): #I004 item_text = self.tree1.item(item,"values") code=item_text[0] status=item_text[3] option=item_text[5] if option=="Y" and status=="Scrap": sql1="SELECT Code量具编码,Vendor供应商 , Description描述 , Type型号, Purchase_Date购买日期, Status状态 FROM instruments_info WHERE Code量具编码='%s'"%(code,) c.execute(sql1) li=c.fetchone() today=(datetime.datetime.now()).strftime("%Y-%m-%d") disposal=item_text[3] li_tuple=tuple(list(li)+[today,disposal]) print(li_tuple) sql2="INSERT INTO scraps (Code量具编码,Vendor供应商 , Description描述 , Type型号, Purchase_Date购买日期,Status状态,Scrap_Date报废日期,Disposal处理方式 ) \ VALUES ('%s','%s','%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s') "%li_tuple print(sql2) c.execute(sql2) sql3="DELETE FROM instruments_info WHERE Code量具编码='%s'"%code print(sql3) c.execute(sql3) count+=1 self.conn.commit() self.conn.close() self.top.withdraw() showinfo('提示', "共保存"+str(count)+"条记录") self.top.deiconify() else: showwarning('警告', "请将状态改为Scrap并选Y") def get_first_from_db(self): self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Code量具编码 FROM instruments_info order by ID ASC LIMIT 1" c.execute(sql) li=c.fetchone()[0] self.conn.commit() self.conn.close() return li if __name__ =="__main__": root = Tk() scrap(root,"usertest","frame") root.mainloop()