QR code generator from Text using Tkinter in Python
Recently I have built a simple QR code generator in Tkinter. I thought it will be fun sharing the tutorial here with you here.
In order to build this QR code generator, we will need these packages:
- Tkinter (It comes along with Python, so you do not need to install it separately)
- pyqrcode
- pypng
- pillow
So at first, let’s install those libraries one by one using pip
pip install pyqrcode pip install pypng pip install pillow
Once you install these one by one, then start the coding.
You can also check: How to set Tkinter Label text through variables
Let me first explain the steps:
- Create a Tkinter window
- Add an entry box or input box so that we can take text input from that.
- Create a button for the submission of that text.
- Create a function and run that when the button is pressed.
In that function, we will create the QR code image and save it in our directory. - Then in the same function, we will show that image in a previously created Label of Tkinter.
Now let’s check the full code:
import tkinter as tk import pyqrcode from PIL import Image, ImageTk newWindow = tk.Tk() qr_label = None def create_qr(): myString = entry.get() my_qr = pyqrcode.create(myString) my_qr.png("my_qr.png", scale=8) print(myString) qr_image = Image.open("my_qr.png") qr_photo = ImageTk.PhotoImage(qr_image) # Update the label with the new image qr_label.config(image=qr_photo) qr_label.image = qr_photo newWindow.geometry("700x700") entry = tk.Entry(newWindow, width=50) entry.pack() myButton = tk.Button(text="Get QR code image", command= create_qr) myButton.pack() qr_label = tk.Label(newWindow) qr_label.pack() newWindow.mainloop()
Output:
Leave a Reply