I tried to make a pop-up save qr code image window using asksaveasfile, but it gives me a corrupted image file.
This is what happens if you open it
My code:
root = Tk()
root.geometry("400x350")
root.title("QR Code Generator")
label = Label(text="QR Code Generator", fg="black", font="NexaHeavy")
label.pack(pady=20)
link_entry = Entry()
link_entry.insert(0, 'Link')
link_entry.pack(pady=50)
name_entry = Entry()
name_entry.insert(0, 'QR Code name')
name_entry.pack(pady=50)
def savefile():
asksaveasfile(defaultextension='*.jpg', filetypes=[
("All types", '.*'),
("JPG File", ".jpg")
])
# Generate QR Code
def make():
qr_code = qrcode.make(link_entry.get())
qr_codename = (f"{name_entry.get()}.png")
qr_code.save(qr_codename, savefile())
make_button = Button(text="Make QR Code", command=make)
make_button.pack(pady=1)
root.mainloop()
CodePudding user response:
If you want to use asksaveasfile()
to select the output filename, you should not construct the filename using qr_codename = (f"{name_entry.get()}.png")
:
def savefile():
# return the selected file object
return asksaveasfile(defaultextension='*.jpg', filetypes=[
("All types", '.*'),
("JPG File", ".jpg")
], mode="wb") # must use binary mode for image data
# Generate QR Code
def make():
qr_codename = savefile()
if qr_codename:
qr_code = qrcode.make(link_entry.get())
qr_code.save(qr_codename)