量具管理-量具生成和二维码打印

tech2024-07-23  57

量具管理-量具生成和二维码打印

只要选择量具分类,按下"生成新量具"按钮可以自动生成量具并能打印二维码 建立文件createclass.py

# -*- coding: utf-8 -*- import re import qrcode import os import sqlite3 import tkinter.ttk from tkinter import * import datetime from PIL import ImageFont from PIL import Image from PIL import ImageDraw from PIL import ImageWin import win32print import win32ui from tkinter.messagebox import * import tkinter.font import baseclass import framebaseclass import constant def qrcreate(content,file_name,uptext): #生成二维码 if not os.path.exists("picture"): os.makedirs("picture") qr = qrcode.QRCode( version=4, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=2,border=25) qr.add_data(content) qr.make(fit=True) img = qr.make_image() img_file = os.getcwd()+"\picture\%s.png"%file_name fonta = ImageFont.truetype("arial.ttf", 13, encoding="utf-8") fontcn =ImageFont.truetype("simhei.ttf", 13, encoding="utf-8") #tkinter.font.Font(family="微软雅黑" ,size=13,weight="bold")# img.save(img_file) #保存二维码在picture文件夹中 im1=Image.open(img_file) draw = ImageDraw.Draw(im1) position=(im1.size[0]-95-draw.textsize(content)[0]/2) position1=(im1.size[0]-90-draw.textsize(content)[0]/2) draw.text((position1,30),uptext,000,font=fontcn) draw.text((position,130),content,000,font=fonta) im1.save(img_file) def printqrcode(content,file_name): #打印函数 HORZRES = 8 VERTRES = 10 LOGPIXELSX = 88 LOGPIXELSY = 90 PHYSICALWIDTH = 110 PHYSICALHEIGHT = 111 PHYSICALOFFSETX = 112 PHYSICALOFFSETY = 113 printer_name = win32print.GetDefaultPrinter () file_name = os.getcwd()+"\picture\%s.png"%file_name hDC = win32ui.CreateDC () hDC.CreatePrinterDC (printer_name) printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES) printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT) printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY) bmp = Image.open (file_name) if bmp.size[0] > bmp.size[1]: bmp = bmp.rotate (0) ratios = [1.0 * printable_area[0] / bmp.size[0], 0.9 * printable_area[1] / bmp.size[1]] scale = min (ratios) hDC.StartDoc (file_name) hDC.StartPage () dib = ImageWin.Dib (bmp) scaled_width, scaled_height = [int (scale * i) for i in bmp.size] x1 = int ((printer_size[0] - scaled_width)/2) y1 = int ((printer_size[1] - scaled_height)) x2 = x1 + scaled_width y2 = y1 + scaled_height dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2)) hDC.EndPage () hDC.EndDoc () hDC.DeleteDC () class create(framebaseclass.baseFrame): def __init__(self,master,user,framename): #继承基类baseFrame self.root=master self.user=user self.framename=framename super( ).__init__(self.root,self.user,self.framename) width = 800 height =600 screenwidth = self.root .winfo_screenwidth() screenheight = self.root .winfo_screenheight() alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2) self.root.geometry(alignstr) self.var_Period=StringVar() self.var_Code=StringVar() self.var_Vendor=StringVar() self.var_Date=StringVar() self.var_Description=self.get_kind_fromdb("instruments_kind","Detail_Name量具名称") self.supplier=constant.SUPPLIER self.createmi_ui() def createmi_ui(self): self.f1=LabelFrame(self.root,text="二维码QRcode",height=190,width=190,labelanchor="n") self.f1.pack(anchor="e",padx=10,pady=2) #使用pack布局 self.combo_Name=tkinter.ttk.Combobox(self.root,value=tuple(self.var_Description)) self.combo_Name.place(x=150,y=40,width=200,height=20) self.combo_Type=tkinter.ttk.Combobox(self.root) self.combo_Type.place(x=150,y=70,width=200,height=20) Label_Code=Label(self.root,text="量具编码Code:") Label_Code.place(x=0,y=10,width=130,height=20) Label_Name=Label(self.root,text="量具分类Name:") Label_Name.place(x=0,y=40,width=130,height=20) Label_Type=Label(self.root,text="型号Type:") Label_Type.place(x=0,y=70,width=130,height=20) Label_Period=Label(self.root,text="校准周期Period(天):") Label_Period.place(x=0,y=100,width=130,height=20) Label_Vendor=Label(self.root,text="供应商Vendor:") Label_Vendor.place(x=0,y=130,width=130,height=20) Label_Date=Label(self.root,text="购买日期Date:") Label_Date.place(x=0,y=160,width=130,height=20) self.entry_Code=Entry(self.root,textvariable=self.var_Code) self.entry_Code.place(x=150,y=10,width=200,height=20) self.entry_Period=Entry(self.root,textvariable=self.var_Period) self.entry_Period.place(x=150,y=100,width=200,height=20) self.entry_Vendor=tkinter.ttk.Combobox(self.root,value=tuple(self.supplier)) self.entry_Vendor.place(x=150,y=130,width=200,height=20) self.entry_Date=Entry(self.root,textvariable=self.var_Date) self.entry_Date.place(x=150,y=160,width=200,height=20) self.listbox=Listbox(self.root) self.listbox.place(x=110,y=300,width=360,height=60) scr=Scrollbar(self.root) scr.place(x=490,y=300,height=60) scr.config(command=self.listbox.yview) self.button_print=Button(self.root,text="打印标签",command=self.print_code) self.button_print.place(x=0,y=300,width=100,height=40) self.button_New=Button(self.root,text="生成新量具",command=self.get_new_instrument) self.button_New.place(x=60,y=200,width=100,height=40) self.button_Save=Button(self.root,text="保存新量具",command=self.save_new_instrument) self.button_Save.place(x=180,y=200,width=100,height=40) self.button_New.bind('<Button-1>',self.callback) self.combo_Type.bind('<Button-1>',self.get_new_type) def print_code(self): #打印二维码 code=self.entry_Code.get() name=self.combo_Name.get() CN = ''.join(re.findall(r'[\u4e00-\u9fa5]+', name)) text=code#内容 file=code#文件名 self.listbox.insert(0,"正在打印:"+file) pic = os.getcwd()+"\picture\%s.png"%file #文件名 if code: qrcreate(text,file,CN) #根据二维码内容生成二维码 self.loadpicture(pic) #调用图片 printqrcode(text,file) #调用打印函数 else: showwarning('数据不全', 'Code量具编码不能为空') def loadpicture(self,pic): #载入图片 global photo photo = PhotoImage(file=pic) #图片 for widget in self.f1.winfo_children(): widget.destroy() theLabel = Label(self.f1,justify=LEFT,image=photo,compound=CENTER, fg="white" ) theLabel.pack() self.root.update() def get_new_instrument(self): #获取新量具 name=self.combo_Name.get() self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Brief_Name量具简称 FROM instruments_kind WHERE Detail_Name量具名称='%s'"%(name,) c.execute(sql) li=c.fetchone()[0] table1="instruments_info" field1="Code量具编码" sql_1="SELECT %s FROM '%s' WHERE %s LIKE '%s%s'" % (field1,table1,field1,li,'%') c.execute(sql_1) li1=c.fetchall() self.conn.commit() self.conn.close() list=[ ] for line in li1: for item in line: list.append(int(item[-3:])) #量具所在类的排名 if list: seq=sorted(list,reverse=True)[0]+1 else: seq=1 new = "%03d" % seq #新量具的后三位 newinstrument=li+new return newinstrument def get_kind_fromdb(self,table,field): #从数据库获取量具种类 list=[] self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT %s FROM %s"%(field,table) c.execute(sql) li=c.fetchall() for line in li: for item in line: list.append(item) self.conn.commit() self.conn.close() return list def get_calibration_period(self): #获取校准周期 try: name=self.combo_Name.get() table="instruments_kind" field="Calibration_Period校准周期" self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT %s FROM %s WHERE Detail_Name量具名称='%s'" %(field, table,name) c.execute(sql) li=c.fetchone()[0] self.conn.commit() self.conn.close() return li except: return def get_new_type(self,event): name=self.combo_Name.get() self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT Brief_Name量具简称 FROM instruments_kind WHERE Detail_Name量具名称='%s'" %name c.execute(sql) li=c.fetchone()[0] type_dict=constant.TYPE kind=type_dict[li] self.conn.commit() self.conn.close() self.combo_Type.set("") self.combo_Type["values"]=kind def get_exist_from_db(self,code): #相同编码是否保存 self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="SELECT * FROM instruments_info WHERE Code量具编码='%s'" %code c.execute(sql) li=c.fetchone() self.conn.commit() self.conn.close() return li def save_new_instrument(self): code=self.entry_Code.get() name=self.combo_Name.get() type_mi=self.combo_Type.get() calibration= self.entry_Period.get() vendor= self.entry_Vendor.get() purchase_date=self.entry_Date.get() if len(code)==0 or len(name)==0 or len(type_mi)==0 or len(calibration)==0 or len(vendor)==0 or len(purchase_date)==0: showwarning('数据不全', '不能有空格') else: if self.get_exist_from_db(code) : showinfo('通知', '量具编号重名,不能保存') else: self.conn=sqlite3.connect("database.db") c=self.conn.cursor() sql="INSERT INTO instruments_info (Code量具编码, Vendor供应商, Description描述, Type型号, Calibration_Period校准周期,Purchase_Date购买日期 ,Status状态)\ VALUES ('%s','%s','%s','%s','%s','%s','%s')"%(code, vendor,name,type_mi,calibration, purchase_date,"Backup") c.execute(sql) self.conn.commit() self.conn.close() showinfo('通知', '量具已保存') self.var_Code.set('') self.var_Period.set('') self.var_Date.set('') self.combo_Name.set('') self.combo_Type.set('') self.entry_Vendor.set('') def callback(self,event): self.var_Code.set(self.get_new_instrument()) self.var_Period.set(self.get_calibration_period()) nowday=(datetime.datetime.now()+datetime.timedelta(days=0)).strftime("%Y-%m-%d") self.var_Date.set(nowday) if __name__ == '__main__': root = Tk() create(root,"usertest","frame") root.mainloop()

最新回复(0)