|
| 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() |
0 commit comments