«Tkinter
A Quick Response Code ( QR Code ) is a machine-readable optical label that contains information about the item to which it is attached. Usually it is an ID or a link or some text which connects to more details.
The data is readable by a scanner. Smart phones can be used as scanner also.
Tkinter QR code generator using pyqrcode library to display and saving images as SVG or PNG file
Libraries required to generate QR code in Tkinter window
There are many libraries available in Python to generate QR code. The reason for going for pyqrcode is the .xbm format image which can easily integrated with Tkinter so no need to save the image first and then display. However we can still save the image in local system by using pypng library.
pip install pyqrcode
To save the image in .png format, we will install pypng
pip install pypng
Generating QR code
Here we are using one string 'plus2net.com' to generate the QR code.
We can use my_img inside a label to display the image ( QR code ) by using config() method. Here we are only displaying the QR code and not storing the image in local system.
l1.config(image=my_img)
Saving as SVG file
We can save the image ( QR code ) in Scalable Vector Graphics (SVG) format
import pyqrcode
from pyqrcode import create
import tkinter as tk
from tkinter import *
import png # pip install pypng ## to save in png format
my_w = tk.Tk()
my_w.geometry("410x400") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
e1=tk.Entry(my_w,font=22,bg='yellow',width=15)
e1.grid(row=0,column=0,padx=10,pady=10)
b1=tk.Button(my_w,font=22,text='Generate QR code',
command=lambda:my_generate())
b1.grid(row=0,column=1,padx=5,pady=10)
l1=tk.Label(my_w,text='QR to display here')
l1.grid(row=1,column=0,columnspan=2)
def my_generate():
global my_img
my_qr = pyqrcode.create(e1.get())
#my_qr.svg('G:\\My Drive\\testing\\my_qr.svg', scale=8)#save svg
my_qr.png('G:\\My Drive\\testing\\my_qr1.png', scale=6,
module_color=[0, 0, 0, 128], background=[0xff, 0xcc, 0xcc])
my_qr.show() # display qr code image in console
my_qr = my_qr.xbm(scale=10)
my_img=tk.BitmapImage(data=my_qr)
l1.config(image=my_img) # Show the qr code in Label
my_w.mainloop() # Keep the window open
Adding logo ( image ) to QR code
Installing PIL
pip install Pillow
Change your path based on local system.
import pyqrcode
from PIL import Image
#url = pyqrcode.QRCode('https://www.plus2net.com',error = 'H')
url = pyqrcode.QRCode('welcome to plus2net',error = 'H')
url.png('D:\\my_qr_code.png',scale=10)
im = Image.open('D:\\my_qr_code.png')
im = im.convert("RGBA")
logo = Image.open("D:\\p2n-logo\\top2.jpg") # path to logo file
box = (100,165,280,200) # dimension for the logo to fix
im.crop(box)
region = logo
region = region.resize((box[2] - box[0], box[3] - box[1]))
im.paste(region,box)
im.show()
im.save("D:\\my_qr_code.png","PNG") # change your path
QR code with logo in Tkinter window
import pyqrcode
from pyqrcode import create
import tkinter as tk
import png # pip install pypng ## to save in png format
from PIL import Image
my_w = tk.Tk()
my_w.geometry("410x400") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
e1=tk.Entry(my_w,font=22,bg='yellow',width=15)
e1.grid(row=0,column=0,padx=10,pady=4)
b1=tk.Button(my_w,font=22,text='Generate QR code',
command=lambda:my_generate())
b1.grid(row=0,column=1,padx=5,pady=4)
l1=tk.Label(my_w,text='QR to display here')
l1.grid(row=1,column=0,columnspan=2)
path='G:\\My Drive\\testing\\my_qr2.png' # Update your path
def my_generate():
global my_img
my_qr = pyqrcode.QRCode(e1.get(),error = 'H')
my_qr.png(path, scale=10,module_color=[0, 0, 0, 128],
background=[0xff, 0xcc, 0xcc])
im = Image.open(path)
im = im.convert("RGBA")
logo = Image.open("D:\\p2n-logo\\top2.jpg")
box = (90,150,250,170) # dimension for the logo to fix
im.crop(box)
region = logo
region = region.resize((box[2] - box[0], box[3] - box[1]))
im.paste(region,box)
im.show() # display in console
im.save("path","PNG")
my_img = tk.PhotoImage(file = "path")
l1.config(image=my_img) # Show the qr code in Label
my_w.mainloop() # Keep the window open