-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
223 lines (203 loc) · 8.01 KB
/
main.py
File metadata and controls
223 lines (203 loc) · 8.01 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import os
import sys
import tkinter as tk
from tkinter import messagebox, filedialog, colorchooser
from ttkbootstrap import Style
import qrcode
from PIL import Image, ImageTk
# Función para manejar rutas de recursos
def resource_path(relative_path):
"""Obtiene la ruta absoluta para los recursos, compatible con PyInstaller"""
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
class QRCodeGeneratorApp:
def __init__(self, root):
# Estilo con ttkbootstrap
self.style = Style(theme="darkly")
self.root = root
self.root.title("Generador de código QR | by @marichu_kt | © 2024")
# Cambiar el icono de la ventana
logo_path = resource_path("logo.ico") # Ruta al archivo de icono
self.root.iconbitmap(logo_path)
# Configurar tamaño y posición de la ventana
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
window_width = 500
window_height = 600
x_position = (screen_width // 2) - (window_width // 2)
y_position = (screen_height // 2) - (window_height // 2)
self.root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
self.root.resizable(False, False)
# Colores predeterminados del QR
self.qr_color = "black"
self.bg_color = "white"
self.setup_ui()
def setup_ui(self):
# Título
self.title_label = tk.Label(
self.root,
text="Generador QR",
font=("Courier new", 24, "bold"),
bg=self.style.colors.bg,
fg=self.style.colors.primary
)
self.title_label.pack(pady=10)
# Frame para texto/URL
text_frame = tk.Frame(self.root, bg=self.style.colors.bg)
text_frame.pack(pady=5)
self.text_label = tk.Label(
text_frame,
text="Texto/URL:",
font=("Courier new", 16, "bold"),
bg=self.style.colors.bg,
fg=self.style.colors.light
)
self.text_label.grid(row=0, column=0, padx=5)
self.text_entry = tk.Entry(
text_frame,
font=("Helvetica", 14),
width=30,
relief="solid"
)
self.text_entry.grid(row=0, column=1, padx=5)
# Frame para botones
button_frame = tk.Frame(self.root, bg=self.style.colors.bg)
button_frame.pack(pady=10)
# Botón con imagen para cambiar tema
theme_icon_path = resource_path("theme.ico") # Ruta al archivo .ico
theme_image = ImageTk.PhotoImage(Image.open(theme_icon_path).resize((27, 27)))
self.theme_button = tk.Button(
button_frame,
image=theme_image,
command=self.toggle_theme,
bg=self.style.colors.bg,
borderwidth=0,
activebackground=self.style.colors.bg
)
self.theme_button.image = theme_image # Necesario para evitar que la imagen sea recolectada por el garbage collector
self.theme_button.pack(side=tk.LEFT, padx=5)
# Color del QR
self.color_button = tk.Button(
button_frame,
text="Color del QR",
command=self.choose_qr_color,
font=("Helvetica", 12, "bold"),
bg=self.style.colors.primary,
fg="white",
activebackground=self.style.colors.info,
relief="solid"
)
self.color_button.pack(side=tk.LEFT, padx=5)
self.bg_button = tk.Button(
button_frame,
text="Color de fondo",
command=self.choose_bg_color,
font=("Helvetica", 12, "bold"),
bg=self.style.colors.primary,
fg="white",
activebackground=self.style.colors.info,
relief="solid"
)
self.bg_button.pack(side=tk.LEFT, padx=5)
self.generate_button = tk.Button(
button_frame,
text="Generar QR",
command=self.generate_qr,
font=("Helvetica", 12, "bold"),
bg=self.style.colors.primary,
fg="white",
activebackground=self.style.colors.info,
relief="solid"
)
self.generate_button.pack(side=tk.LEFT, padx=5)
# Canvas para mostrar el QR
self.qr_canvas = tk.Canvas(
self.root,
width=300,
height=300,
bg=self.style.colors.dark,
highlightthickness=0
)
self.qr_canvas.pack(pady=15)
# Botón Guardar
self.save_button = tk.Button(
self.root,
text="Guardar Código QR",
command=self.save_qr,
font=("Helvetica", 12, "bold"),
bg=self.style.colors.success,
fg="white",
activebackground=self.style.colors.info,
relief="solid"
)
self.save_button.pack(pady=2)
# Créditos
self.credit_label = tk.Label(
self.root,
text="by @marichu_kt | © 2024",
font=("Helvetica", 10),
bg=self.style.colors.bg,
fg=self.style.colors.light
)
self.credit_label.pack(side=tk.BOTTOM, pady=2)
def toggle_theme(self):
current_theme = self.style.theme_use()
if current_theme == "darkly":
self.style.theme_use("flatly")
else:
self.style.theme_use("darkly")
def choose_qr_color(self):
color = colorchooser.askcolor(title="Seleccionar color del código QR")
if color[1]:
self.qr_color = color[1]
def choose_bg_color(self):
color = colorchooser.askcolor(title="Seleccionar color de fondo del QR")
if color[1]:
self.bg_color = color[1]
def generate_qr(self):
content = self.text_entry.get()
if not content:
messagebox.showwarning("Advertencia", "Por favor, introduce un texto o URL.")
return
try:
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(content)
qr.make(fit=True)
qr_img = qr.make_image(fill_color=self.qr_color, back_color=self.bg_color)
qr_img = qr_img.resize((300, 300))
self.qr_image = ImageTk.PhotoImage(qr_img)
self.qr_canvas.create_image(150, 150, image=self.qr_image)
except Exception as e:
messagebox.showerror("Error", f"No se pudo generar el código QR. {e}")
def save_qr(self):
if not hasattr(self, "qr_image"):
messagebox.showwarning("Advertencia", "Por favor, genera un código QR primero.")
return
try:
content = self.text_entry.get()
filetypes = [("PNG", "*.png"), ("JPEG", "*.jpeg"), ("JPEG", "*.jpg")]
filename = filedialog.asksaveasfilename(defaultextension=".png", filetypes=filetypes)
if filename:
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(content)
qr.make(fit=True)
qr_img = qr.make_image(fill_color=self.qr_color, back_color=self.bg_color)
qr_img.save(filename)
messagebox.showinfo("Éxito", f"Código QR guardado como '{filename}'.")
except Exception as e:
messagebox.showerror("Error", f"No se pudo guardar el código QR. {e}")
# Iniciar la aplicación
if __name__ == "__main__":
root = tk.Tk()
app = QRCodeGeneratorApp(root)
root.mainloop()