I am learning from a tutorial online about how to generate QR Codes. I have started to create the frontend which is the application window with an input field and text label. I am now onto the backend which is the code for the 'Generate' button which allows the user to generate the QR Code with the inputted URL to assign the inputted URL with the QR Code.
I have previously tried to attempt to open the image of the QR Code in the window body area of the application and my code keeps on invoking the following error: AttributeError: type object 'Image' has no attribute 'open'
. I currently am expecting it to open the Image in my application window so that I can scan it to ensure the QR Code redirects me to the website I have assigned it to but it instead causes the program to invoke the above error.
Python Code as it currently stands:
import qrcode
from PIL import Image
from tkinter import *
root = Tk()
root.geometry('500x500')
root.title('QR Code Generator Application')
root.resizable(0,0)
Label(root, text='QR Code Generator Application', font='Helvetica 18 bold').place(x=65, y=20)
Label(root, text='Enter anything into the input field: ', font='Helvetica 12').place(x=20, y=90)
Entry(root, width=25).place(x=250, y=93)
def button():
qr_code= qrcode.QRCode(version=2, box_size=10, border=4)
img = qr_code.make_image(fill='Black', back_color='Red')
img.save('QR_Code_1.png')
Image.open('QR_Code_1.png')
Button(root, text='Generate', font='Helvetica 15 bold', bg='Green', command=button).place(x=183, y=140)
root.mainloop()
If there is any way anyone could possibly help out a fellow beginner programmer, I'd be glad to consider any suggestions about the above code and how to solve the problem which I am currently having.
Thank you :)
Due to receiving negative votes. You should update to Python 3.12.7, because of bugfree and beyond.
I have previously tried to attempt to open the image of the QR Code in the window body area of the application and my code keeps on invoking the following error: AttributeError: type object 'Image' has no attribute 'open'.
tkinter import *
library. Use
import tkinter as tk
and then use the tk.
prefix.You ready to go.
Snippet:
import qrcode
from PIL import ImageTk, Image
import tkinter as tk
root = tk.Tk()
root.geometry('500x500')
root.title('QR Code Generator Application')
root.resizable(0,0)
tk.Label(root, text='QR Code Generator Application', font='Helvetica 18 bold').place(x=65, y=20)
tk.Label(root, text='Enter anything into the input field: ', font='Helvetica 12').place(x=20, y=90)
tk.Entry(root, width=25).place(x=250, y=93)
def button():
qr_code= qrcode.QRCode(version=2, box_size=10, border=4)
img = qr_code.make_image(fill='Black', back_color='Red')
img.save('dog.png')
ImageTk.PhotoImage(Image.open('dog.png'))
tk.Button(root, text='Generate', font='Helvetica 15 bold', bg='Green', command=button).place(x=183, y=140)
root.mainloop()
screenshot original image:
Screenshot for Qrcode: