-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (86 loc) · 3.82 KB
/
main.py
File metadata and controls
112 lines (86 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Python QR Code Generator
Author: ImEasooon
License: MIT
GitHub: https://github.com/ImEasooon
Description:
A simple GUI-based QR code generator built with Python that allows users to generate QR codes from text and save them as image files.
"""
import sys
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import qrcode
import qrcode.constants
from PIL import ImageTk
class QRGenerator(tk.Tk):
def __init__(self):
super().__init__()
self.inputValue = None
# QR Object
self.qr = None
# OR Code PIL
self.code = None
# Tkinter Image (Showing)
self.image = None
# Title
self.title("QR Code Generator")
self.CenterWindowPosition(400, 500)
self.resizable(False, False)
self.protocol("WM_DELETE_WINDOW", self.ExitApplication)
# Input Frame
self.contentFrame = ttk.LabelFrame(self, text="Content Input")
self.contentFrame.pack(padx=10, pady=10, fill="both")
self.contentFrame.grid_anchor("center")
# Source input
ttk.Label(self.contentFrame, text="Data/ URL: ").grid(row=0, column=0, padx=5, pady=10)
self.userInput = ttk.Entry(self.contentFrame, width=30)
self.userInput.grid(row=0, column=1, padx=5, pady=10)
# Generate button
self.generateButton = ttk.Button(self.contentFrame, text="Generate", command=self.GenerateQR)
self.generateButton.grid(row=0, column=2, padx=10, pady=10)
# QR frame
self.outputFrame = ttk.LabelFrame(self, text="Generated QR Code")
self.outputFrame.pack(padx=10, pady=10, fill="both")
self.outputFrame.grid_anchor("center")
# Show the generated QR Code
self.output = tk.Canvas(self.outputFrame, width=300, height=300)
self.output.grid(row=0, column=0, padx=10, pady=10)
# Save button
self.saveButton = ttk.Button(self.outputFrame, text="Save", width=25, command=self.SaveQR)
self.saveButton.grid(row=1, column=0, padx=10, pady=10)
self.saveButton.grid_anchor("center")
def CenterWindowPosition(self, width, height):
windowWidth = self.winfo_screenwidth()
windowHeight = self.winfo_screenheight()
positionX = (windowWidth // 2) - (width // 2)
positionY = (windowHeight // 2) - (height // 2)
self.geometry(f"{width}x{height}+{positionX}+{positionY}")
def GenerateQR(self):
self.inputValue = self.userInput.get()
if not self.inputValue:
messagebox.showwarning("Warning", "Please input any value before generating!")
return
self.qr = qrcode.QRCode(version=4, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=10, border=4)
self.qr.add_data(self.inputValue)
self.qr.make(fit=True)
self.code = self.qr.make_image(fill_color="black", back_color="white")
self.code = self.code.convert("RGB")
self.code = self.code.resize((300, 300))
self.image = ImageTk.PhotoImage(self.code)
self.output.create_image(0, 0, anchor="nw", image=self.image)
def SaveQR(self):
if not self.qr:
messagebox.showwarning("Warning", "Please generate a qr code before downloading!")
return
filePath = filedialog.asksaveasfilename(defaultextension="png", initialfile="qrcode", filetypes=[("PNG", "*.png"), ("JPEG", "*.jpeg"), ("JPG", "*.jpg")], title="Save As")
if filePath:
self.code.save(filePath)
messagebox.showinfo("Success", f"QR code has been saved to {filePath}")
else:
messagebox.showerror("Error", "Failed to save the qr code! Please try again.")
def ExitApplication(self):
self.quit()
sys.exit()
if __name__ == "__main__":
App = QRGenerator()
App.mainloop()