Generating QR Codes with Python and PySimpleGUI

Posted by


QR (Quick Response) codes are two-dimensional barcodes that can store a variety of information, such as URLs, contact information, or plain text. In this tutorial, we will learn how to generate QR codes using Python and PySimpleGUI.

PySimpleGUI is a Python library that allows you to easily create GUI applications using a simple and intuitive syntax. We will utilize this library to create a simple GUI application where users can input text and generate a QR code based on that text.

To get started, first make sure you have Python installed on your system. You can download Python from the official website (https://www.python.org/downloads/).

Next, install the PySimpleGUI library by running the following command in your terminal:

pip install PySimpleGUI

Once you have installed PySimpleGUI, you can proceed to create your QR code generator application. Here’s the code for the application:

import PySimpleGUI as sg
import qrcode

layout = [
    [sg.Text('Enter text to generate QR code:')],
    [sg.InputText(key='-INPUT-')],
    [sg.Button('Generate QR Code'), sg.Button('Exit')],
    [sg.Image(key='-IMAGE-')]
]

window = sg.Window('QR Code Generator', layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

    if event == 'Generate QR Code':
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=10,
            border=4,
        )
        qr.add_data(values['-INPUT-'])
        qr.make(fit=True)

        img = qr.make_image(fill_color="black", back_color="white")
        img.save('qrcode.png')

        window['-IMAGE-'].update('qrcode.png')

window.close()

In this code, we create a simple GUI window using PySimpleGUI with an input text field, a button to generate the QR code, and an image element to display the generated QR code. When the user clicks the "Generate QR Code" button, we create a QR code based on the input text using the qrcode library and display the generated QR code on the GUI window.

To run the application, save the code to a Python file (e.g., qrcode_generator.py) and run it in your terminal:

python qrcode_generator.py

You should see a GUI window with an input text field, a button to generate the QR code, and an image element to display the generated QR code. Enter some text in the input field, click the "Generate QR Code" button, and you should see the QR code displayed on the window.

That’s it! You have successfully created a QR code generator application using Python and PySimpleGUI. Feel free to customize the application further, add error handling, or explore additional features of the qrcode library to enhance the functionality of the application.

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x