Tutorial 18: Create a QR Code Generator GUI App Using Python PySimpleGUI

Posted by


In this tutorial, we will be creating a QR Code Generator GUI app using Python and the PySimpleGUI library. PySimpleGUI is a simple and easy-to-use GUI framework for Python that allows you to create GUIs with minimal code. We will be using the qrcode library to generate QR codes in our app.

To get started, make sure you have Python and PySimpleGUI installed on your machine. You can install PySimpleGUI using pip:

pip install PySimpleGUI

Next, install the qrcode library:

pip install qrcode

Now, let’s start by importing the required libraries:

import PySimpleGUI as sg
import qrcode

Next, let’s define the layout of our GUI app:

layout = [
    [sg.Text('Enter text or URL to generate QR code:')],
    [sg.InputText(key='input')],
    [sg.Button('Generate QR Code'), sg.Button('Exit')],
    [sg.Image(key='output')]
]

In the layout, we have a text input field for the user to enter the text or URL for which they want to generate a QR code. We also have a button to generate the QR code and another button to exit the app. Lastly, we have an Image element where we will display the generated QR code.

Now, let’s create the window for our app using the layout:

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

Next, let’s define a function to generate the QR code:

def generate_qr_code(text):
    qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
    qr.add_data(text)
    qr.make(fit=True)
    img = qr.make_image(fill_color='black', back_color='white')
    img.save('temp_qr_code.png')
    return 'temp_qr_code.png'

In this function, we create a QR code object using the qrcode library and generate the QR code for the input text. We then save the generated QR code as a temporary image file and return the file path.

Now, let’s create a loop to handle events in our GUI app:

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Generate QR Code':
        text = values['input']
        qr_code_file = generate_qr_code(text)
        window['output'].update(filename=qr_code_file)

In this loop, we check for events such as the user closing the window or clicking the ‘Exit’ button. If the user clicks the ‘Generate QR Code’ button, we get the input text from the input field, generate the QR code using the generate_qr_code function, and update the Image element with the generated QR code.

Finally, let’s close the window when the loop exits:

window.close()

And that’s it! You have now created a simple QR Code Generator GUI app using Python and PySimpleGUI. Feel free to customize the app further by adding more features or improving the design. Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@turtlecode
28 days ago

✅ Python PySimpleGUI Full Playlist : https://www.youtube.com/playlist?list=PLMi6KgK4_mk1kGWXRif1naLejNdYDS_t2