Skip to content

MasterBhuvnesh/QR_code_generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QR Code Generator

This Python program uses Tkinter and qrcode libraries to generate and display a QR code in a user-friendly GUI. The user inputs a URL and a name for the QR code, which is then generated and displayed in a new window. The user can save the QR code to their system

Installation :-

Import the qrcode library for generating QR codes

Install all required module by :-

pip install qrcode,pillow,os-sys

qrcode module is imported for generating QR codes.
tkinter module is imported for creating a GUI.
filedialog module is imported from tkinter for displaying the file dialog box to select the folder.
PIL (Python Imaging Library) is imported for handling images.
os module is imported for interacting with the operating system.

import qrcode  
from tkinter import *
from tkinter import filedialog,messagebox
from PIL import ImageTk, Image
import os

Create a Tkinter window for user Input

An instance of Tkinter Tk class is created and assigned to the variable root The window title is set to "Input Form" The size of the window is set to 400x300 pixels

root = Tk()
root.title("Input Form")
root.geometry("400x300")

Add an icon to GUI

icon is set with iconbitmap

root.iconbitmap("icon.ico")

Add labels and entry boxes for URL and Name input

Four instances of Tkinter Label and Entry classes are created to display and input the URL and name

label_url = Label(root, text="Enter URL:")
label_url.pack(pady=5)
input_url = Entry(root)
input_url.pack(pady=5)
label_name = Label(root, text="Enter Name:")
label_name.pack(pady=5)
input_name = Entry(root)
input_name.pack(pady=5)

Define a function to get user Input

A function get_input() is defined to get the input values from the user and assign them to global variables user_input_url and user_input_name A button is created to submit the input values by calling the get_input() function

def get_input():
    global user_input_url,user_input_name
    user_input_url = input_url.get()
    user_input_name = input_name.get()

If URL not given it will show message

    if not user_input_url:
        messagebox.showerror("Error", "You have not given URL")
        return

If name not given it will show message

    if not user_input_name:
        messagebox.showerror("Error", "You have not given name")
        return
    

Print the URL and name

    print("User URL:   ", user_input_url  )
    print("User Name:  ", user_input_name  )

To close the current GUI

    root.destroy()

Add a button to submit user input

submit_button = Button(root, text="Submit", command=get_input)
submit_button.pack(pady=25)

Run the Tkinter event loop to display the input window

root.mainloop()

If name or url not given it will exit

if not user_input_url or not user_input_name:
    exit()

Create a QR code using the qrcode library

An instance of the QRCode class is created with version 1, box size 10, and border 5 The input URL is assigned to the variable data The add_data() method of the QRCode instance is called with data as an argument to add the input URL to the QR code The make() method is called with fit=True to make the QR code fit the data The make_image() method is called to generate the QR code as an image with black fill color and white background color The generated image is saved as a PNG file named "qr_code.png"

qr = qrcode.QRCode(version=1, box_size=10, border=5)
data = user_input_url 
qr.add_data(data)
qr.make(fit=True)

This generates the QR code image with the specified colors and saves it as a PNG file named "qr_code.png"

img = qr.make_image(fill_color="black", back_color="white")

Save the QR code as a PNG file named "qr_code.png"

img.save("qr_code.png")

Create a new Tkinter window to display the QR code

A new instance of the Tk class is created for displaying the QR code, and assigned to the variable root_qr The window title is set to the input name The background color of the window is set to white

root_qr = Tk()
root_qr.title(f'{user_input_name}')
root_qr.config(bg="white")

Add an icon to GUI

root_qr.iconbitmap("icon.ico")

Open the saved image and get its width and height

This opens the QR code image file and gets its dimensions, and sets global variables for the width and height. ```bash with Image.open("qr_code.png") as img: width, height = img.size global Width,Height Width=(width) Height=(height) ```

Define a function to delete the saved image when the window is closed

This defines a function to delete the QR code image file and close the window when the user closes the window.

def delete_file_on_close():
    os.remove("qr_code.png")
    root_qr.destroy()

Define a function to prompt the user to select a directory to save the file

location defines a function to prompt the user to select a directory to save the QR code image, and then saves the image as a PNG file in that directory with the specified filename (user's input name + ".png"). It also renames the file from "qr_code.png" to the specified filename.

def location():
    desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
    fi=filedialog.askdirectory(title="Where you want to save ?",initialdir=desktop)
    img.save(f'{fi}/qr_code.png')

Rename the saved file to match the user input for the file name

    current_file_name = "qr_code.png"
    file_name=user_input_name+".png"
    new_file_name = file_name
    directory_path = (f'{fi}/')
    file_path = os.path.join(directory_path, current_file_name)
    os.rename(file_path, os.path.join(directory_path, new_file_name))

Display the image in a Label widget

This opens the QR code image file and creates a PhotoImage object for displaying it in the window. It then creates a label with the image and adds a button to save the image using the location function.

img = Image.open("qr_code.png")
photo = ImageTk.PhotoImage(img)
label = Label(root_qr, image=photo, width=Width, height=Height,background="white")
label.pack()

Button

Add a button to prompt the user to select a directory to save the file

poke=Button(root_qr,text="save",borderwidth=0,font="20",bg="white",command=location,activebackground="white")
poke.pack(pady=10)

Set a function to be called when the window is closed

This sets the window protocol to call the delete_file_on_close function when the user closes the window.

root_qr.protocol("WM_DELETE_WINDOW", delete_file_on_close)

Run the Tkinter event loop to display QR code

This starts the main event loop for the window, which handles user interactions with the window and updates its display.

root_qr.mainloop()

Author

Badges

Github License

Screenshots

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages