基于tkinter、qrcode设计的二维码生成器。用于提高网址等少量文本由PC端向手机端传输的效率问题。支持二维码的尺寸、容错率、像素密度、Logo图片设置,以及参数纠错、二维码的大图查看和保存。
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 10 13:49:14 2019
@author: Belth Pixtink
Project: QR Code maker
Version: 1.0.0
"""
import os
import qrcode
from tkinter import *
from PIL import Image,ImageTk
qr = qrcode.QRCode(
version=5,#QR Size:version x 4 + 17,1<= version <=40
error_correction=qrcode.constants.ERROR_CORRECT_H,#L:7%,M:15%,Q:25%,H:30%
box_size=4,#size of pixie in the box
border=1#width of the boder
)
QR_Data = "QR Maker--> Copyright by @BelthPixtink version:Beta-1.0.0"
FileName = 'QR_Icon.png'
root = Tk() #初始化Tk()
root.title("二维码生成器")
root.geometry('400x820')
root.configure(background = 'lightgray')
Blank1 = Label(root, text=" ",bg = 'lightgray')
Blank1.pack()
l1 = Label(root, text="二维码内容",bg = "lightgray")
l1.pack() #side:LEFT RTGHT TOP BOTTOM
Text_text = StringVar()
Text = Entry(root, textvariable = Text_text)
Text_text.set(QR_Data)
Text.pack()
Blank2 = Label(root, text=" ",bg = 'lightgray')
Blank2.pack()
l2 = Label(root, text="二维码尺寸:1~40",bg = "lightgray")
l2.pack()
Version_text = StringVar()
Version = Entry(root, textvariable = Version_text)
Version_text.set("5")
Version.pack()
Blank3 = Label(root, text=" ",bg = 'lightgray')
Blank3.pack()
l3 = Label(root, text="容错率:H-30% Q-25% M-15% L-7%",bg = "lightgray")
l3.pack()
Error_text = StringVar()
Error = Entry(root, textvariable = Error_text)
Error_text.set("H")
Error.pack()
Blank4 = Label(root, text=" ",bg = 'lightgray')
Blank4.pack()
l4 = Label(root, text="像素密度:1~10",bg = "lightgray")
l4.pack()
Box_text = StringVar()
Box = Entry(root, textvariable = Box_text)
Box_text.set("4")
Box.pack()
Blank5 = Label(root, text=" ",bg = 'lightgray')
Blank5.pack()
l5 = Label(root, text="Logo尺寸:1~10",bg = "lightgray")
l5.pack()
Border_text = StringVar()
Border = Entry(root, textvariable = Border_text)
Border_text.set("1")
Border.pack()
IMGP = Image.open(FileName)
IMGP = IMGP.resize((300,300))
IMGP = ImageTk.PhotoImage(IMGP)
IGP = Label(root, image = IMGP, bg = "black")
IGP.pack()
IGP.place(x=50,y=470,width=300,height=300)
Blank6 = Label(root, text=" ",bg = 'lightgray')
Blank6.pack()
File_text = StringVar()
File = Entry(root, textvariable = File_text)
File_text.set(FileName)
File.pack()
def SelectImg():
global FileName
FileName = tkinter.filedialog.askopenfilename()
if FileName == '':
FileName = 'QR_Icon.png'
File_text.set(FileName)
File.pack()
def QRShow():
global FileName,img
QR_Data = Text_text.get()
Ve = Version_text.get()
Er = Error_text.get()
Bx = Box_text.get()
Br = Border_text.get()
if Ve.isdigit() == False:
Ve = "5"
elif (int(Ve) < 1) | (int(Ve) > 40):
Ve = "5"
if Er.isalpha() == False:
Er = "H"
elif (Er != "H") & (Er != "Q") & (Er != "M") & (Er != "L") & (Er != "h") & (Er != "q") & (Er != "m") & (Er != "l"):
Er = "H"
if Bx.isdigit() == False:
Bx = "4"
elif (int(Bx) < 1) | (int(Bx) > 10):
Bx = "4"
if Br.isdigit() == False:
Br = "1"
elif (int(Br) < 1) | (int(Br) > 10):
Br = "1"
Text_text.set(QR_Data)
Text.pack()
Version_text.set(str(Ve))
Version.pack()
Error_text.set(str(Er))
Error.pack()
Box_text.set(str(Bx))
Box.pack()
Border_text.set(str(Br))
Border.pack()
qr.clear()
qr.box_size = int(Bx)
qr.version = int(Ve)
qr.border = int(Br)
if (Er == "H") | (Er == "h"):
qr.error_correction = qrcode.constants.ERROR_CORRECT_H
elif (Er == "Q") | (Er == "q"):
qr.error_correction = qrcode.constants.ERROR_CORRECT_Q
elif (Er == "M") | (Er == "m"):
qr.error_correction = qrcode.constants.ERROR_CORRECT_Q
elif (Er == "L") | (Er == "l"):
qr.error_correction = qrcode.constants.ERROR_CORRECT_Q
qr.add_data(QR_Data)
qr.make(fit=True)
img = qr.make_image()
img = img.convert("RGBA")
img_w, img_h = img.size
factor = 3
size_w = int(img_w / factor)
size_h = int(img_h / factor)
icon = Image.open(FileName)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
w = int((img_w - icon_w) / 2)
h = int((img_h - icon_h) / 2)
img.paste(icon, (w, h), icon)
if img.size[0] > 336:
IMGP = img.resize((336,336))
else:
IMGP = img
IMGP = ImageTk.PhotoImage(IMGP)
IGP = Label(root, image = IMGP, bg = "black")
IGP.IMGP = IMGP
IGP.pack()
IGP.place(x=32,y=470,width=336,height=336)
def SaveImg():
Path = tkinter.filedialog.askdirectory()
img.save(os.path.join(Path,'QR_IMG.png'))
def ShowImg():
img.show()
def main():
Button0 = Button(root, text="选择Logo图片.png", command = SelectImg,bg = "lightblue").pack()
l6 = Label(root, text="*----------------------------------\
----------------------------------------*",bg = 'lightgray')
l6.pack()
Button1 = Button(root, text="生成QR", command = QRShow,bg = "pink")
Button1.pack()
Button1.place(x=70,y=425,width=70)
Button2 = Button(root, text="保存QR", command = SaveImg,bg = "lightgreen")
Button2.pack()
Button2.place(x=165,y=425,width=70)
Button3 = Button(root, text="查看QR", command = ShowImg,bg = "lightyellow")
Button3.pack()
Button3.place(x=260,y=425,width=70)
root.mainloop()
if __name__ == "__main__":
main()
运行界面(测试环境—python3.7):
== ---------------------------------------------------------华丽的分割线---------------------------------------------------------==