Skip to content
Advertisement

How do I change the colour of my QR code?

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?

from tkinter import *
import os
import pyqrcode

window = Tk()
window.geometry("550x350")
window.configure(bg="#0434a0")
window.title("QR Code Maker")
photo = PhotoImage(file = "logo.png")
window.iconphoto(False, photo)

def generate():
    if len(Subject.get())!=0 :
        global qr,photo
        qr = pyqrcode.create(Subject.get())
        photo = BitmapImage(data = qr.xbm(scale=8))
    else:
        messagebox.showinfo("Voer een URL in...")
    try:
        showcode()
    except:
        pass

def showcode():
    imageLabel.config(image = photo)
    subLabel.config(text="QR van " + Subject.get())

Advertisement

Answer

pyqrcode module has not been updated for over 5 years. Use qrcode module instead. Note that qrcode module requires Pillow module.

...
from PIL import ImageTk
import qrcode
...
def generate():
    try:
        subject = Subject.get().strip()
        if len(subject) != 0:
            # adjust border and box_size to suit your case
            qr = qrcode.QRCode(border=2, box_size=10)
            qr.add_data(subject)
            # change fill_color and back_color to whatever you want
            img = qr.make_image(fill_color='blue', back_color='cyan')
            photo = ImageTk.PhotoImage(img)
            showcode(subject, photo)
        else:
            messagebox.showinfo("Voer een URL in...")
    except Exception as ex:
        print(ex)

def showcode(subject, photo):
    imageLabel.config(image=photo)
    imageLabel.image = photo # keep a reference of the image to avoid garbage collection
    subLabel.config(text="QR van " + subject)
...

Note that I pass the photo and subject to showcode() instead of using global variables.

Suggest to merge the code in showcode() into generate().

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement