Project : QR Generator Python code
Name :Asiya , Khadija , Tooba
from tkinter import*
import qrcode
from PIL import Image, ImageTk
class QrGenerator:
def __init__(self, root):
self.img = None
self.root = root
self.root.geometry(“900×500+200+200”)
self.root.title(“QR Generator”)
self.root.maxsize(900, 500)
title = Label(self.root, text=” Qr Code generator”, font=(“times new roman”, 40), bg=”navy blue”, fg=”white”, anchor=”w”)
title.place(x=0, y=0, relwidth=1)
self.var_stu_code = StringVar()
self.var_name = StringVar()
self.var_department = StringVar()
self.var_session = StringVar()
self.var_gmail = StringVar()
stu_frame = Frame(self.root, bd=2, relief=RIDGE, bg=”white”)
stu_frame.place(x=50, y=100, width=500, height=380)
stu_title = Label(stu_frame, text=”Student Details”, font=(“times new roman”, 20), bg=”navy blue”, fg=”white”)
stu_title.place(x=0, y=0, relwidth=1)
label_stu_code = Label(stu_frame, text=”Student ID”, font=(“times new roman”, 15, ‘bold’), bg=”white”)
label_stu_code.place(x=20, y=60)
label_name = Label(stu_frame, text=”Name”, font=(“times new roman”, 15, ‘bold’), bg=”white”)
label_name.place(x=20, y=100)
label_program = Label(stu_frame, text=”Program”, font=(“times new roman”, 15, ‘bold’), bg=”white”)
label_program.place(x=20, y=140)
label_session = Label(stu_frame, text=”Session”, font=(“times new roman”, 15, ‘bold’), bg=”white”)
label_session.place(x=20, y=180)
label_gmail = Label(stu_frame, text=”Gmail”, font=(“times new roman”, 15, ‘bold’), bg=”white”)
label_gmail.place(x=20, y=220)
txt_stu_code = Entry(stu_frame, font=(“times new roman”, 15), textvariable=self.var_stu_code, bg=”light yellow”)
txt_stu_code.place(x=200, y=60)
txt_name = Entry(stu_frame, font=(“times new roman”, 15), textvariable=self.var_name, bg=”light yellow”)
txt_name.place(x=200, y=100)
txt_program = Entry(stu_frame, font=(“times new roman”, 15), textvariable=self.var_department, bg=”light yellow”)
txt_program.place(x=200, y=140)
txt_session = Entry(stu_frame, font=(“times new roman”, 15), textvariable=self.var_session, bg=”light yellow”)
txt_session.place(x=200, y=180)
txt_gmail = Entry(stu_frame, font=(“times new roman”, 15), textvariable=self.var_gmail, bg=”light yellow”)
txt_gmail.place(x=200, y=220)
button_generate = Button(stu_frame, text=”Generate QR”, command=self.generate, font=(“times new roman”, 18, ‘bold’), bg=”sky blue”)
button_generate.place(x=90, y=270, width=180, height=30)
button_clear = Button(stu_frame, text=”clear”, command=self.clear, font=(“times new roman”, 18, ‘bold’), bg=”sky blue”)
button_clear.place(x=282, y=270, width=120, height=30)
self.msg = “”
self.lbl_msg = Label(self.root, text=self.msg, font=(“times new roman”, 18), bg=”white”, fg=”green”)
self.lbl_msg.place(x=135, y=420)
# —qr code window—
qr_frame = Frame(self.root, bd=2, relief=RIDGE, bg=”white”)
qr_frame.place(x=600, y=100, width=250, height=380)
qr_title = Label(qr_frame, text=”Student QR Code”, font=(“times new roman”, 20), bg=”navy blue”, fg=”white”)
qr_title.place(x=0, y=0, relwidth=1)
self.qr_code = Label(qr_frame, text=”no QR \n available”, font=(“times new roman”, 15), bg=’blue’, fg=’white’, bd=1, relief=RIDGE)
self.qr_code.place(x=35, y=100, width=180, height=180)
def clear(self):
self.var_stu_code.set(”)
self.var_name.set(”)
self.var_department.set(”)
self.var_session.set(”)
self.var_gmail.set(”)
self.msg = “”
self.lbl_msg.config(text=self.msg)
self.qr_code.config(image=””)
def generate(self):
if self.var_stu_code.get() == ” or self.var_name.get() == ” or self.var_department.get() == ” or self.var_session.get() == ” or self.var_gmail.get() == ”:
self.msg = ” All fields are required!!”
self.lbl_msg.config(text=self.msg, font=20, fg=’red’)
else:
qr_data = f”Student ID: {self.var_stu_code.get()}\nName: {self.var_name.get()}\nDepartment: {self.var_department.get()}\nSession: {self.var_session.get()}\nGmail: {self.var_gmail.get()}”
qr_code = qrcode.make(qr_data)
qr_code.save(“Student_qr/stu_” + str(self.var_stu_code.get()) + ‘.png’)
self.img = ImageTk.PhotoImage(file=”Student_qr/stu_” + str(self.var_stu_code.get()) + ‘.png’)
new_width = 180
new_height = 180
qr_code = qr_code.resize((new_width, new_height), Image.LANCZOS)
qr_code.save(“Student_qr/stu_” + str(self.var_stu_code.get()) + ‘.png’)
# —QR code image update—
self.img = ImageTk.PhotoImage(file=”Student_qr/stu_” + str(self.var_stu_code.get()) + ‘.png’)
self.qr_code.config(image=self.img)
# —updating notification—
self.msg = “QR code generated successfully!!”
self.lbl_msg.config(text=self.msg, fg=’green’)
root = Tk()
obj = QrGenerator(root)
root.mainloop()