I want to make a QR code with python. The output is a white background with a black qr code. How do I change both of them?
JavaScript
x
27
27
1
from tkinter import *
2
import os
3
import pyqrcode
4
5
window = Tk()
6
window.geometry("550x350")
7
window.configure(bg="#0434a0")
8
window.title("QR Code Maker")
9
photo = PhotoImage(file = "logo.png")
10
window.iconphoto(False, photo)
11
12
def generate():
13
if len(Subject.get())!=0 :
14
global qr,photo
15
qr = pyqrcode.create(Subject.get())
16
photo = BitmapImage(data = qr.xbm(scale=8))
17
else:
18
messagebox.showinfo("Voer een URL in...")
19
try:
20
showcode()
21
except:
22
pass
23
24
def showcode():
25
imageLabel.config(image = photo)
26
subLabel.config(text="QR van " + Subject.get())
27
Advertisement
Answer
pyqrcode
module has not been updated for over 5 years. Use qrcode
module instead. Note that qrcode
module requires Pillow
module.
JavaScript
1
26
26
1
2
from PIL import ImageTk
3
import qrcode
4
5
def generate():
6
try:
7
subject = Subject.get().strip()
8
if len(subject) != 0:
9
# adjust border and box_size to suit your case
10
qr = qrcode.QRCode(border=2, box_size=10)
11
qr.add_data(subject)
12
# change fill_color and back_color to whatever you want
13
img = qr.make_image(fill_color='blue', back_color='cyan')
14
photo = ImageTk.PhotoImage(img)
15
showcode(subject, photo)
16
else:
17
messagebox.showinfo("Voer een URL in...")
18
except Exception as ex:
19
print(ex)
20
21
def showcode(subject, photo):
22
imageLabel.config(image=photo)
23
imageLabel.image = photo # keep a reference of the image to avoid garbage collection
24
subLabel.config(text="QR van " + subject)
25
26
Note that I pass the photo and subject to showcode()
instead of using global variables.
Suggest to merge the code in showcode()
into generate()
.