1 # -*- encoding = utf-8 -*- 2 from numpy.lib.npyio import save 3 import qrcode 4 from tkinter import * 5 from tkinter import messagebox 6 from tkinter import filedialog 7 import tkinter as tk 8 import os 9 from PIL import Image, ImageTk 10 import cv2 11 import numpy as np 12 13 14 def inspection(): 15 string = entry_txt.get() 16 img = qrcode.make(string) 17 18 filename = filedialog.asksaveasfilename( 19 defaultextension='.png') 20 img.save(filename) 21 messagebox.showinfo('提示', '%s保存成功' % filename) 22 23 24 def attention(): 25 messagebox.showinfo( 26 '使用说明', '输入文本内容转换为二维码\n按下保存图片保存二维码\n') 27 28 29 def refreshText(): 30 # 生成二维码 31 string = entry_txt.get() 32 33 if string != '': 34 global img_png 35 36 save_img = qrcode.make(string) 37 num_img = 255 * np.array(save_img).astype('uint8') 38 img = cv2.cvtColor(np.asarray(num_img), cv2.COLOR_RGB2BGR) 39 resize_img = cv2.resize(img, (300, 300)) 40 img = Image.fromarray(cv2.cvtColor(resize_img, cv2.COLOR_BGR2RGB)) 41 42 img_png = ImageTk.PhotoImage(img) 43 label_image = Label(root, image=img_png) 44 label_image.grid(row=2, column=0, padx=10, pady=10) 45 46 root.after(500, refreshText) 47 48 49 if __name__ == "__main__": 50 root = Tk() 51 e = StringVar() 52 root.title('二维码生成器') 53 root.geometry('360x470') 54 55 entry_txt = Entry(root, font=('微软雅黑', 20)) 56 entry_txt.grid(row=0, column=0, padx=20, pady=30) 57 58 Label_image = Label(root) 59 Label_image.grid(row=2, column=0, padx=20, pady=150) 60 61 save_button = Button(root, text='保存图片', 62 font=('微软雅黑', 15), 63 fg='red', 64 command=inspection) 65 save_button.grid(row=4, sticky=SE) 66 67 use_guide_button = Button(root, 68 text='使用说明', 69 font=('微软雅黑', 15), 70 command=attention) 71 use_guide_button.grid(row=4, sticky=SW) 72 73 root.after(500, refreshText) 74 root.mainloop()
浙公网安备 33010602011771号