17.pyhton驱动电脑安装的zebra打印机机通过ZPL指令打印
1 #encoding:utf-8 2 3 from tkinter import * 4 from tkinter import messagebox 5 import win32print 6 import time 7 from tkinter import scrolledtext 8 9 # 控制输入框只能输入数字且小于8位数 10 def validate_op(input_text): 11 if input_text.isdigit() or input_text == "": 12 if len(input_text) <= 8: 13 return True 14 return False 15 16 # 控制输入框只能输入数字且小于4位数 17 def validate_password(input_text): 18 if input_text.isdigit() or input_text == "": 19 if len(input_text) <= 4: 20 return True 21 return False 22 23 24 def get_printers(): 25 printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS) 26 printer_list = [] 27 for printer in printers: 28 printer_name = printer[2] 29 printer_list.append(printer_name) 30 return printer_list 31 32 class Application(Frame): 33 def __init__(self, master=None): 34 super().__init__(master) 35 self.master = master 36 self.pack() 37 38 self.createWidget() 39 40 def createWidget(self): 41 self.utk_l_title = Label(root,text="OBA RM LABEL 列印指令生成",bg="red",font=("Arial",16),width=30,height=2).pack() 42 self.utk_l_DB = Label(root,text="请选择打印机:",font=("Arial",16)).place(x = 30,y = 70) 43 #创建一个选择数据库下拉框 46 PSHDB = ["请选择打印机"] 47 # 调用函数获取打印机列表 48 printer_list = get_printers() 49 # 打印机列表打印输出 50 for printer in printer_list: 51 # print(printer) 52 PSHDB.append(printer) 53 self.PSHDBVariable = StringVar() 54 self.PSHDBVariable.set(PSHDB[0]) 55 self.utk_o_DB = OptionMenu(root,self.PSHDBVariable, *PSHDB).place(x = 180,y = 70) 56 #创建一个工号标签 57 self.utk_l_op = Label(root,text="开始序号:",font=("Arial",16)).place(x = 30,y = 120) 58 self.utk_l_op1 = Label(root, text="【年份+日期+0000】如 23080000", font=("Arial", 12)).place(x=290, y=125) 59 #创建一个工号输入框 60 # self.utk_e_op=Entry(root) 61 self.utk_e_op = Entry(root, validate="key", validatecommand=(root.register(validate_op), '%P')) 62 self.utk_e_op.place(x = 150,y = 125) 63 self.utk_e_op.bind("<Return>", self.clear_entry) 64 #创建一个密码标签 65 self.utk_l_password = Label(root,text="打印数量:",font=("Arial",16)).place(x = 30,y = 170) 66 self.utk_l_password1 = Label(root, text="【1~1000】", font=("Arial", 12)).place(x=290, y=170) 67 #创建一个密码输入框 68 self.utk_e_password = Entry(root, validate="key", validatecommand=(root.register(validate_password), '%P')) 69 self.utk_e_password.place(x = 150,y = 175) 70 self.utk_e_password.bind("<Return>", self.clear_entry) 71 #创建一个登录按钮 72 self.utk_b_login = Button(root,text="打印",font=("Arial",12),width=10,height=1) 73 self.utk_b_login.place(x = 150,y = 210) 74 self.utk_b_login["command"] = self.songhua 75 #创建一个提示信息标签 76 self.utk_l_info = Label(root,text="快乐工作,幸福生活!!!",font=("Arial",16)) 77 self.utk_l_info.place(x = 30,y = 250) 78 79 def clear_entry(self, event): 80 event.widget.delete(0, END) 81 82 def songhua(self): 83 """判断登录填写是否符合要求""" 84 self.tnsName = self.PSHDBVariable.get().strip() 85 self.name = self.utk_e_op.get().strip() 86 self.password = self.utk_e_password.get().strip() 87 if self.tnsName=="请选择打印机": 88 messagebox.showwarning("警告", "必须选择打印机!!!") 89 elif len(self.name)==0 and self.name=="": 90 messagebox.showwarning("警告", "请输入开始序号") 91 elif len(self.name)!=8: 92 messagebox.showwarning("警告", "开始序号必须8位数") 93 elif self.name.startswith('0'): 94 messagebox.showwarning("警告", "输入的数不能以0开头") 95 elif len(self.password)==0 and self.password=="": 96 messagebox.showwarning("警告", "请输入打印数量") 97 elif int(self.password) > 1000: 98 messagebox.showwarning("警告", "打印数量必须在1~1000") 99 elif int(self.password) < 0: 100 messagebox.showwarning("警告", "打印数量必须在1~1000") 101 else: 102 messagebox.showinfo("打印信息", "恭喜你,输入格式正确,"+self.tnsName+",开始序号:"+self.name+",打印数量是:"+self.password) 103 # self.linkOrcale(self.name,self.password,self.tnsName) 104 a = int(self.name) 105 b = '' 106 d = [] 107 file = open('data.txt', 'w') 108 for i in range(int(self.password)): 109 a +=1 110 c = str(a) 111 b = '^XA^FO158,170^A@N,35,33,MYRIAD_SET_BOLD_.FNT^FDRM0' + c + '^FS ^FO185,35^BXN,8,400,,,^FDRM0' + c + '^FS^PQ1^XZ' 112 # print(b) 113 d.append(b) 114 file.write(b+'\n') 115 file.close() 116 # print(d) 117 print("数据已成功保存到文件 data.txt 中")
118 zpl_code = "^XA^FO50,50^A0N,50,50^FDHello, World!^FS^XZ" 119 selected_printer = win32print.OpenPrinter(self.tnsName) # 获取选择的打印机句柄 120 win32print.StartDocPrinter(selected_printer, 1, ("Print Job", None, None)) # 开始打印任务 121 win32print.StartPagePrinter(selected_printer) # 开始打印一页 122 win32print.WritePrinter(selected_printer, zpl_code.encode('utf-8')) # 发送ZPL代码到打印机 123 win32print.EndPagePrinter(selected_printer) # 结束打印一页 124 time.sleep(1) # 添加1秒的延迟 125 win32print.EndDocPrinter(selected_printer) # 结束打印任务 126 win32print.ClosePrinter(selected_printer) # 关闭打印机 127 128 129 if __name__ == "__main__": 130 root = Tk() 131 root.geometry("550x300+200+300") 132 root.title("Label打印小工具") 133 app = Application(master=root) 134 root.mainloop()