Skip to content

Commit bbca02f

Browse files
Merge pull request #2305 from smty2018/qr
QR Code generator GUI
2 parents cb731cf + 8e76f0b commit bbca02f

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

QR Code generator GUI/generator.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#pip install qrcode[pil]
2+
3+
import qrcode
4+
import tkinter as tk
5+
from tkinter import filedialog
6+
from PIL import Image, ImageTk
7+
8+
def generate_qr_code(data, filename='qr_code.png', scale=10):
9+
10+
try:
11+
qr = qrcode.QRCode(
12+
version=None,
13+
error_correction=qrcode.constants.ERROR_CORRECT_L,
14+
box_size=scale,
15+
border=4,
16+
)
17+
qr.add_data(data)
18+
qr.make(fit=True)
19+
qr_image = qr.make_image(fill_color="black", back_color="white")
20+
qr_image.save(filename)
21+
22+
return filename
23+
24+
except Exception as e:
25+
print(f"Error generating QR code: {e}")
26+
return None
27+
28+
def generate_qr_button_click():
29+
qr_data = qr_data_entry.get()
30+
qr_image = generate_qr_code(qr_data)
31+
if qr_image:
32+
img = Image.open(qr_image)
33+
photo = ImageTk.PhotoImage(img)
34+
35+
qr_label.config(image=photo)
36+
qr_label.image = photo
37+
38+
result_label.config(text="QR code generated successfully.")
39+
else:
40+
result_label.config(text="Failed to generate the QR code.")
41+
42+
def save_qr_button_click():
43+
qr_data = qr_data_entry.get()
44+
qr_filename = filedialog.asksaveasfilename(defaultextension=".png",
45+
filetypes=[("PNG Files", "*.png"), ("All Files", "*.*")])
46+
if qr_filename:
47+
qr_image = generate_qr_code(qr_data, qr_filename)
48+
if qr_image:
49+
result_label.config(text=f"QR code image saved to: {qr_image}")
50+
else:
51+
result_label.config(text="Failed to save the QR code.")
52+
else:
53+
result_label.config(text="QR code download canceled.")
54+
55+
56+
root = tk.Tk()
57+
root.title("QR Code Generator")
58+
59+
60+
root.configure(bg='#1E1E1E')
61+
root.option_add('*foreground', 'white')
62+
root.option_add('*activeForeground', 'white')
63+
root.option_add('*background', '#1E1E1E')
64+
root.option_add('*activeBackground', '#444444')
65+
root.option_add('*highlightBackground', '#444444')
66+
root.option_add('*selectBackground', '#444444')
67+
root.option_add('*selectForeground', 'white')
68+
root.option_add('*font', 'Helvetica 12')
69+
70+
71+
qr_data_label = tk.Label(root, text="Enter the data for QR code:")
72+
qr_data_label.pack(pady=5)
73+
74+
qr_data_entry = tk.Entry(root, width=40)
75+
qr_data_entry.pack(pady=5)
76+
77+
generate_qr_button = tk.Button(root, text="Generate QR Code", command=generate_qr_button_click)
78+
generate_qr_button.pack(pady=10)
79+
80+
qr_label = tk.Label(root)
81+
qr_label.pack(pady=10)
82+
83+
save_qr_button = tk.Button(root, text="Save QR Code", command=save_qr_button_click)
84+
save_qr_button.pack(pady=5)
85+
86+
result_label = tk.Label(root, text="", fg="white")
87+
result_label.pack(pady=5)
88+
89+
90+
root.mainloop()

QR Code generator GUI/readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#requirements:
2+
pip install qrcode[pil]
3+
4+
#Demo:
5+
https://github.com/smty2018/Amazing-Python-Scripts/assets/74114936/1e52c8ee-7eb1-4bbc-9c9c-e5dacc26dd1b
6+

0 commit comments

Comments
 (0)