Generate QR Codes with PySimpleGUI

Posted by


In this tutorial, we will explore how to use PySimpleGUI to generate QR codes in Python. PySimpleGUI is a user-friendly graphical user interface (GUI) library that simplifies the process of creating GUIs in Python.

To get started, you will need to install PySimpleGUI by running the following command in your terminal or command prompt:

pip install PySimpleGUI

Now, let’s go ahead and create a simple GUI application that generates QR codes. First, import the necessary modules:

import PySimpleGUI as sg
import pyqrcode

Next, define the layout of your GUI using PySimpleGUI elements. In this example, we will create a simple window with an input field for the user to enter the text for the QR code, a button to generate the QR code, and an area to display the QR code image:

layout = [
    [sg.Text('Enter text for QR code:')],
    [sg.InputText()],
    [sg.Button('Generate QR Code')],
    [sg.Image(key='qr_image')]
]

Then, create an instance of the PySimpleGUI window using the layout and set the window title:

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

Now, let’s define a function that generates the QR code image when the user clicks the "Generate QR Code" button. Inside the function, we will retrieve the text from the input field, create a QR code object using the pyqrcode library, and convert it to an image using the PNG format:

def generate_qr_code(text):
    qr = pyqrcode.create(text)
    qr.png('qr_code.png', scale=8)
    return 'qr_code.png'

Finally, create a loop that listens for events from the GUI and updates the QR code image when the user clicks the button:

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

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Generate QR Code':
        text = values[0]
        qr_image_path = generate_qr_code(text)
        window['qr_image'].update(filename=qr_image_path)

window.close()

That’s it! You have successfully created a simple GUI application that generates QR codes using PySimpleGUI. You can further customize the layout and design of the GUI to meet your specific requirements.

I hope you found this tutorial helpful! Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@junojan8414
1 hour ago

I get an error :
File "D:pyhtonexcelcodeQR_code_in_ui.py", line 61, in generate_qr_code

img.save(path)

^^^^^^^^

AttributeError: 'tuple' object has no attribute 'save'

1
0
Would love your thoughts, please comment.x
()
x