QR code Generator using python.

Here we will creator a QR code generator using python. It will be easy to code and use.

So, we got two ways to do this first one is straightforward to code and the second one is also simple but a little bit time-consuming. And as usual you will find full code at the bottom of the post.

Method 1.

import qrcode
img = qrcode.make("www.codelikenull.com")
img.save("codelikenullQR.jpg")

Yes, this is it. only 3 lines of code, first of you have to install "qrcode" so you can use pip or you can go to setting and then interpreter then click on add package to install this in IDE's.

This code will create an image name "codelikenullQR.jpg" which contains the link (www.codelikenull.com). So if you want to share with anyone else then you use this. Like this:-

qr code

Method 2.

The second method is a little bit typical but it also has its benefits.

In the second one, we will be using "Tkinter"(to make this QR code generator in GUI form) and "pyqrcode" you can install them using pip. After installing, use this code to import all the variables we need.

from tkinter import *
from tkinter import messagebox
import pyqrcode

And for setting up Tkinter we will use this code:-

ws = Tk()
ws.title("QR code")
ws.config(bg='#46c768')

Yep, you can guess this is for decoration also.

Now we will create a function named "generate_QR" which will utilize "pyqrcode" and uses input to generate a QR code which will be displayed to the user using Tkinter later.

def generate_QR():
if len(user_input.get()) != 0:
global qr, img
qr = pyqrcode.create(user_input.get())
img = BitmapImage(data=qr.xbm(scale=8))
else:
messagebox.showwarning('warning', 'All Fields are Required!')
try:
display_code()
except:
pass

Here, we have used bitmap-image to create QR codes. Now we will create a new function to display QR codes.

def display_code():
img_lbl.config(image=img)
output.config(text="QR code of " + user_input.get())

And now let's finish the code with connecting functions to Tkinter.

lbl = Label(
ws,
text="Enter message or URL",
bg='#46c768'
)
lbl.pack()

user_input = StringVar()
entry = Entry(
ws, textvariable=user_input
)
entry.pack(padx=60)

button = Button(
ws, text="generate_QR",
width=25,
command=generate_QR
)
button.pack(pady=10)
img_lbl = Label(
ws,
bg='#fff')
img_lbl.pack()
output = Label(
ws, text="",
bg='#46c768' )
output.pack()
ws.mainloop()

Now we have finished if you try to run this code a GUI will pop up and you have to input a web link or text to generate a QR code. This won't save the QR code like the previous code was doing but you can save these QR codes using file IO and other functions. Here is a screenshot of the GUI:-

qr code 2

Here is the full code:-

"""

import qrcode
img = qrcode.make("www.codelikenull.com")
img.save("codelikenullQR.jpg")
"""
from tkinter import *
from tkinter import messagebox
import pyqrcode

ws = Tk()
ws.title("QR code")
ws.config(bg='#46c768')


def generate_QR():
if len(user_input.get()) != 0:
global qr, img
qr = pyqrcode.create(user_input.get())
img = BitmapImage(data=qr.xbm(scale=8))
else:
messagebox.showwarning('warning', 'All Fields are Required!')
try:
display_code()
except:
pass



def display_code():
img_lbl.config(image=img)
output.config(text="QR code of " + user_input.get())


lbl = Label(
ws,
text="Enter message or URL",
bg='#46c768'
)
lbl.pack()

user_input = StringVar()
entry = Entry(
ws,
textvariable=user_input
)
entry.pack(padx=60)

button = Button(
ws,
text="generate_QR",
width=25,
command=generate_QR
)
button.pack(pady=10)

img_lbl = Label(
ws,
bg='#fff')
img_lbl.pack()
output = Label(
ws,
text="",
bg='#46c768'
)
output.pack()

ws.mainloop()

Share this Page!

Browse from our featured topics

© Copyright 2022 codelikenull - All Rights Reserved