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())
CodePudding user response:
The documentation of this module can be found here: PyQRCode Module Documentation
It says that the various methods of this module (e.g. png
, if you want to get the QR code as a png image) take a background
parameter that lets you define the background color, as well as a module_color
parameter for the code itself.
CodePudding user response:
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()
.