-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
596 lines (467 loc) · 23.4 KB
/
gui.py
File metadata and controls
596 lines (467 loc) · 23.4 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import tkinter as tk
from tkinter import ttk, messagebox
from tkinter.simpledialog import askstring
from config import CURRENT_USER
from crypto import md5
from database import add_credential, get_credentials, delete_credential, initialize_database
from utils import (
validatePasskey, storeLoginCredentials, isUserValid, checkPasswordStrength,
update_strength_meter, generate_and_display_password, copy_password_to_clipboard
)
# Defining constant main window sizes
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 400
PADDING = 20
BUTTON_WIDTH = 20
ENTRY_WIDTH = 30
# sub-window sizes
SUB_WINDOW_WIDTH = 500
SUB_WINDOW_HEIGHT = 400
# Color scheme - using standard web colors for better compatibility
PRIMARY_COLOR = "#4a6fa5" # Blue
SECONDARY_COLOR = "#e8f1f5" # Light blue
ACCENT_COLOR = "#166088" # Darker blue
TEXT_COLOR = "#333333" # Dark gray
BG_COLOR = "#f5f5f5" # Light gray
BUTTON_BG = PRIMARY_COLOR
BUTTON_FG = "white"
HEADER_BG = PRIMARY_COLOR
HEADER_FG = "white"
# Credits information
CREDITS = "Made by Muhammad Hammad"
def center_window(window, width, height):
"""Center a window on the screen and set minimum size."""
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = (screen_width - width) // 2
y = (screen_height - height) // 2
window.geometry(f"{width}x{height}+{x}+{y}")
# Set minimum size to prevent UI elements from being cut off
window.minsize(width, height)
# Update the window to ensure proper rendering
window.update_idletasks()
def create_form_field(parent, label_text, show=None, width=ENTRY_WIDTH):
"""Create a standardized form field with label and entry."""
frame = tk.Frame(parent, bg=BG_COLOR)
frame.pack(fill=tk.X, padx=PADDING, pady=5)
label = tk.Label(frame, text=label_text, width=15, anchor="w",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
label.pack(side=tk.LEFT)
entry = tk.Entry(frame, show=show, width=width, font=("Arial", 10),
bg="white", fg=TEXT_COLOR, relief="solid", bd=1)
entry.pack(side=tk.LEFT, expand=True, fill=tk.X)
return entry
def create_button(parent, text, command, is_primary=False, is_danger=False):
"""Create a standardized button with consistent styling."""
if is_danger:
bg_color = "#e74c3c" # Red for dangerous actions
active_bg = "#c0392b" # Darker red for hover
elif not is_primary:
bg_color = "#7f8c8d" # Gray for secondary actions
active_bg = "#2c3e50" # Darker gray for hover
else:
bg_color = BUTTON_BG
active_bg = ACCENT_COLOR
button = tk.Button(
parent,
text=text,
command=command,
width=BUTTON_WIDTH,
bg=bg_color,
fg=BUTTON_FG,
font=("Arial", 10, "bold" if is_primary else "normal"),
relief="flat",
activebackground=active_bg,
activeforeground="white",
padx=10,
pady=5
)
return button
def add_credits(parent):
"""Add credits at the bottom of the window."""
credits_frame = tk.Frame(parent, bg=BG_COLOR)
credits_frame.pack(side=tk.BOTTOM, fill=tk.X, pady=5)
credits_label = tk.Label(
credits_frame,
text=CREDITS,
bg=BG_COLOR,
fg="#888888", # Light gray for subtle appearance
font=("Arial", 8, "italic")
)
credits_label.pack(side=tk.RIGHT, padx=10)
def setup_user(root) -> None:
"""Handles user setup through the GUI."""
user_window = tk.Toplevel(root)
user_window.title("Set Up User")
user_window.transient(root)
user_window.grab_set()
center_window(user_window, SUB_WINDOW_WIDTH, SUB_WINDOW_HEIGHT+50)
user_window.configure(bg=BG_COLOR)
# Header
header_frame = tk.Frame(user_window, bg=HEADER_BG, pady=10)
header_frame.pack(fill=tk.X)
header_label = tk.Label(header_frame, text="Create New User",
font=("Arial", 16, "bold"), bg=HEADER_BG, fg=HEADER_FG)
header_label.pack(pady=(PADDING, 10))
# Content frame
content_frame = tk.Frame(user_window, bg=BG_COLOR)
content_frame.pack(fill=tk.BOTH, expand=True, padx=PADDING, pady=PADDING)
# Username field
username_entry = create_form_field(content_frame, "Username:")
# Password field
password_frame = tk.Frame(content_frame, bg=BG_COLOR)
password_frame.pack(fill=tk.X, padx=PADDING, pady=5)
password_label = tk.Label(password_frame, text="Password:", width=15, anchor="w",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
password_label.pack(side=tk.LEFT)
password_entry = tk.Entry(password_frame, show="*", width=ENTRY_WIDTH,
font=("Arial", 10), bg="white", fg=TEXT_COLOR,
relief="solid", bd=1)
password_entry.pack(side=tk.LEFT, expand=True, fill=tk.X)
# Generated password display
generated_frame = tk.Frame(content_frame, bg=BG_COLOR)
generated_frame.pack(fill=tk.X, padx=PADDING, pady=5)
password_display = tk.Label(content_frame, text="Generated Password: Not generated yet",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
password_display.pack(padx=PADDING, pady=5)
# Strength meter
strength_frame = tk.Frame(content_frame, bg=BG_COLOR)
strength_frame.pack(fill=tk.X, padx=PADDING, pady=5)
strength_label = tk.Label(strength_frame, text="Strength: Not checked", width=30, anchor="w",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
strength_label.pack(side=tk.LEFT)
progress = ttk.Progressbar(strength_frame, style="danger.Horizontal.TProgressbar",
length=200, mode='determinate')
progress.pack(side=tk.LEFT, expand=True, fill=tk.X, padx=(10, 0))
# Generate password button
def generate_password_and_display():
generated_password = generate_and_display_password(password_entry, password_display, strength_label, progress)
return generated_password
# Buttons frame
button_frame = tk.Frame(content_frame, bg=BG_COLOR)
button_frame.pack(fill=tk.X, padx=PADDING, pady=PADDING)
# Create another frame inside for centered buttons
center_frame = tk.Frame(button_frame, bg=BG_COLOR)
center_frame.pack(expand=True) # This centers the inner frame
generate_button = create_button(center_frame, "Generate Password", generate_password_and_display)
generate_button.pack(side=tk.LEFT, padx=5)
# Copy password button
def copy_password():
password = password_entry.get()
copy_password_to_clipboard(password)
copy_button = create_button(center_frame, "Copy Password", copy_password)
copy_button.pack(side=tk.LEFT, padx=5)
# Update strength meter on password change
def on_password_change(*args):
password = password_entry.get()
update_strength_meter(password, strength_label, progress)
password_entry.bind("<KeyRelease>", on_password_change)
# Submit the user data
def validate_and_store_user():
username = username_entry.get()
password = password_entry.get()
if not username:
messagebox.showerror("Error", "Username is required.")
return
# Validate password strength
is_valid, reason = validatePasskey(password)
if not is_valid:
messagebox.showerror("Error", reason)
return
try:
# We're now passing the raw password to add_user
from database import add_user
add_user(username, password)
messagebox.showinfo("Setup Complete", "User setup successfully. You can now log in.")
user_window.destroy()
except Exception as e:
messagebox.showerror("Error", f"Failed to set up user: {e}")
# 'Submit' button to submit the credentials
submit_frame = tk.Frame(content_frame, bg=BG_COLOR)
submit_frame.pack(fill=tk.X, padx=PADDING, pady=(0, PADDING))
# Button container for side-by-side buttons
button_container = tk.Frame(submit_frame, bg=BG_COLOR)
button_container.pack(pady=10)
submit_button = create_button(button_container, "Create User", validate_and_store_user, is_primary=True)
submit_button.pack(side=tk.LEFT, padx=5)
# Cancel button
cancel_button = create_button(button_container, "Cancel", user_window.destroy, is_primary=False)
cancel_button.pack(side=tk.LEFT, padx=5)
# Add credits
add_credits(user_window)
def validate_user(root, show_main_interface) -> None:
"""Handles user validation through the GUI with a single form."""
login_window = tk.Toplevel(root)
login_window.title("Login")
login_window.transient(root)
login_window.grab_set()
center_window(login_window, SUB_WINDOW_WIDTH, SUB_WINDOW_HEIGHT)
login_window.configure(bg=BG_COLOR)
# Header
header_frame = tk.Frame(login_window, bg=HEADER_BG, pady=10)
header_frame.pack(fill=tk.X)
header_label = tk.Label(header_frame, text="Login to Password Manager",
font=("Arial", 14, "bold"), bg=HEADER_BG, fg=HEADER_FG)
header_label.pack(pady=(PADDING, 10))
# Content frame
content_frame = tk.Frame(login_window, bg=BG_COLOR)
content_frame.pack(fill=tk.BOTH, expand=True, padx=PADDING, pady=PADDING)
# Username field
username_entry = create_form_field(content_frame, "Username:")
# Password field
password_entry = create_form_field(content_frame, "Password:", show="*")
def perform_login():
username = username_entry.get()
password = password_entry.get()
if not username or not password:
messagebox.showerror("Error", "Username and password are required.")
return
if isUserValid(username, password):
CURRENT_USER.set(username)
login_window.destroy()
messagebox.showinfo("Success", "Login successful!")
show_main_interface(root)
# Analyze passwords after successful login
from analysis import analyzePasswords
strength_summary = analyzePasswords()
if strength_summary:
messagebox.showinfo("Password Analysis", strength_summary)
else:
messagebox.showerror("Error", "Invalid username or password.")
# Login button - FIX: Make it full width like other buttons
button_frame = tk.Frame(content_frame, bg=BG_COLOR)
button_frame.pack(fill=tk.X, padx=PADDING, pady=PADDING)
# Button container for side-by-side buttons
button_container = tk.Frame(button_frame, bg=BG_COLOR)
button_container.pack(pady=10)
login_button = create_button(button_container, "Login", perform_login, is_primary=True)
login_button.pack(side=tk.LEFT, padx=5)
# Cancel button
cancel_button = create_button(button_container, "Cancel", login_window.destroy, is_primary=False)
cancel_button.pack(side=tk.LEFT, padx=5)
# Bind Enter key to login
login_window.bind("<Return>", lambda event: perform_login())
# Add credits
add_credits(login_window)
def logout(main_window: tk.Toplevel, root: tk.Tk) -> None:
"""Logs out the current user and returns to the login/setup screen."""
CURRENT_USER.set("None") # Reset the current user
CURRENT_USER.set_key(None) # Reset the encryption key
main_window.destroy() # Close the main interface window
root.deiconify() # Redisplay the root login/setup window
def add_credential_window() -> None:
"""Adds a new credential with password strength meter."""
# Open a new window to add credentials
credential_window = tk.Toplevel()
credential_window.title("Add Credential")
# Use the standard sub-window size
center_window(credential_window, SUB_WINDOW_WIDTH, SUB_WINDOW_HEIGHT + 120)
credential_window.configure(bg=BG_COLOR)
# Header
header_frame = tk.Frame(credential_window, bg=HEADER_BG, pady=10)
header_frame.pack(fill=tk.X)
header_label = tk.Label(header_frame, text="Add New Credential",
font=("Arial", 14, "bold"), bg=HEADER_BG, fg=HEADER_FG)
header_label.pack(pady=(PADDING, 10))
# Content frame
content_frame = tk.Frame(credential_window, bg=BG_COLOR)
content_frame.pack(fill=tk.BOTH, expand=True, padx=PADDING, pady=PADDING)
# Fields to input the website, username, and password
website_entry = create_form_field(content_frame, "Website:")
username_entry = create_form_field(content_frame, "Username:")
password_entry = create_form_field(content_frame, "Password:", show="*")
# Strength meter
strength_frame = tk.Frame(content_frame, bg=BG_COLOR)
strength_frame.pack(fill=tk.X, padx=PADDING, pady=5)
strength_label = tk.Label(strength_frame, text="Strength: Not checked", width=30, anchor="w",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
strength_label.pack(side=tk.LEFT)
progress = ttk.Progressbar(strength_frame, style="danger.Horizontal.TProgressbar", length=200, mode='determinate')
progress.pack(side=tk.LEFT, expand=True, fill=tk.X, padx=(10, 0))
# Function to update password strength meter
def on_password_change(*args):
password = password_entry.get()
update_strength_meter(password, strength_label, progress)
password_entry.bind("<KeyRelease>", on_password_change)
def submit_credential():
website = website_entry.get()
username = username_entry.get()
password = password_entry.get()
# Check if any field is empty
if not website or not username or not password:
messagebox.showerror("Error", "All fields are required.")
return
# Validate password strength
strength, reason = checkPasswordStrength(password)
summary = strength + " Password! " + reason
messagebox.showinfo("Password Analysis", summary)
try:
# Store the credential using the new database function
add_credential(website, username, password)
messagebox.showinfo("Success", "Credential added successfully!")
credential_window.destroy()
except Exception as e:
messagebox.showerror("Error", f"Failed to add credential: {e}")
# Submit button
submit_frame = tk.Frame(content_frame, bg=BG_COLOR)
submit_frame.pack(fill=tk.X, padx=PADDING, pady=(0, PADDING))
# Button container for side-by-side buttons
button_container = tk.Frame(submit_frame, bg=BG_COLOR)
button_container.pack(pady=10)
submit_button = create_button(button_container, "Add Credential", submit_credential, is_primary=True)
submit_button.pack(side=tk.LEFT, padx=5)
# Cancel button
cancel_button = create_button(button_container, "Cancel", credential_window.destroy, is_primary=False)
cancel_button.pack(side=tk.LEFT, padx=5)
# Add credits
add_credits(credential_window)
def view_credentials() -> None:
"""Displays all saved credentials with search functionality."""
credentials_window = tk.Toplevel()
credentials_window.title("View Credentials")
center_window(credentials_window, SUB_WINDOW_WIDTH, SUB_WINDOW_HEIGHT + 120)
credentials_window.configure(bg=BG_COLOR)
# Header
header_frame = tk.Frame(credentials_window, bg=HEADER_BG, pady=10)
header_frame.pack(fill=tk.X)
header_label = tk.Label(header_frame, text="Your Saved Credentials",
font=("Arial", 14, "bold"), bg=HEADER_BG, fg=HEADER_FG)
header_label.pack(pady=(PADDING, 10))
# Content frame
content_frame = tk.Frame(credentials_window, bg=BG_COLOR)
content_frame.pack(fill=tk.BOTH, expand=True, padx=PADDING, pady=PADDING)
# Search frame
search_frame = tk.Frame(content_frame, bg=BG_COLOR)
search_frame.pack(fill=tk.X, padx=PADDING, pady=5)
search_label = tk.Label(search_frame, text="Search:", width=10, anchor="w",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
search_label.pack(side=tk.LEFT)
search_entry = tk.Entry(search_frame, width=ENTRY_WIDTH, font=("Arial", 10),
bg="white", fg=TEXT_COLOR, relief="solid", bd=1)
search_entry.pack(side=tk.LEFT, expand=True, fill=tk.X)
# Results frame with scrollbar
results_frame = tk.Frame(content_frame, bg=BG_COLOR)
results_frame.pack(fill=tk.BOTH, expand=True, padx=PADDING, pady=10)
# Add scrollbar
scrollbar = tk.Scrollbar(results_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Use Text widget instead of Label for better scrolling
credentials_text = tk.Text(results_frame, wrap=tk.WORD, yscrollcommand=scrollbar.set,
height=15, width=50, font=("Arial", 10),
bg="white", fg=TEXT_COLOR, relief="solid", bd=1)
credentials_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=credentials_text.yview)
def search_credentials():
"""Filters and displays credentials based on the search query."""
query = search_entry.get().lower()
credentials_text.delete(1.0, tk.END) # Clear previous results
try:
# Get credentials from the database, filtered by search term if provided
credentials = get_credentials(query if query else None)
if not credentials:
credentials_text.insert(tk.END, "No credentials found.")
return
# Display credentials with index numbers
for i, cred in enumerate(credentials, 1):
# Apply tags for styling
credentials_text.insert(tk.END, f"{i}. Website: ", "index")
credentials_text.insert(tk.END, f"{cred['website']}\n", "website")
credentials_text.insert(tk.END, f" Username: ", "label")
credentials_text.insert(tk.END, f"{cred['username']}\n", "value")
credentials_text.insert(tk.END, f" Password: ", "label")
credentials_text.insert(tk.END, f"{cred['password']}\n\n", "value")
# Configure tags
credentials_text.tag_configure("index", font=("Arial", 10, "bold"), foreground=PRIMARY_COLOR)
credentials_text.tag_configure("website", font=("Arial", 10, "bold"))
credentials_text.tag_configure("label", font=("Arial", 10), foreground=TEXT_COLOR)
credentials_text.tag_configure("value", font=("Arial", 10))
except Exception as e:
messagebox.showerror("Error", f"Failed to load credentials: {e}")
# Search button
search_button = create_button(search_frame, "Search", search_credentials)
search_button.config(width=10) # Override width for this specific button
search_button.pack(side=tk.LEFT, padx=(10, 0))
# Bind the search functionality to the key release event for dynamic search
search_entry.bind("<KeyRelease>", lambda event: search_credentials())
# Load and display all credentials initially
search_credentials()
# Close button
button_frame = tk.Frame(credentials_window, bg=BG_COLOR)
button_frame.pack(fill=tk.X, padx=PADDING, pady=10)
close_button = create_button(button_frame, "Close", credentials_window.destroy)
close_button.pack(pady=10)
# Add credits
add_credits(credentials_window)
def delete_credential_window() -> None:
"""Deletes a credential by ID with improved UI."""
delete_window = tk.Toplevel()
delete_window.title("Delete Credential")
# Use the standard sub-window size
center_window(delete_window, SUB_WINDOW_WIDTH, SUB_WINDOW_HEIGHT + 120)
delete_window.configure(bg=BG_COLOR)
# Header
header_frame = tk.Frame(delete_window, bg=HEADER_BG, pady=10)
header_frame.pack(fill=tk.X)
header_label = tk.Label(header_frame, text="Delete Credential",
font=("Arial", 14, "bold"), bg=HEADER_BG, fg=HEADER_FG)
header_label.pack(pady=(PADDING, 10))
# Content frame
content_frame = tk.Frame(delete_window, bg=BG_COLOR)
content_frame.pack(fill=tk.BOTH, expand=True, padx=PADDING, pady=PADDING)
# Instructions
instructions = tk.Label(content_frame,
text="Enter the index number of the credential to delete.\nYou can view the index numbers in the View Credentials screen.",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
instructions.pack(padx=PADDING, pady=5)
# Index entry
index_frame = tk.Frame(content_frame, bg=BG_COLOR)
index_frame.pack(fill=tk.X, padx=PADDING, pady=10)
index_label = tk.Label(index_frame, text="Credential Index:", width=15, anchor="w",
bg=BG_COLOR, fg=TEXT_COLOR, font=("Arial", 10))
index_label.pack(side=tk.LEFT)
index_entry = tk.Entry(index_frame, width=10, font=("Arial", 10),
bg="white", fg=TEXT_COLOR, relief="solid", bd=1)
index_entry.pack(side=tk.LEFT)
def perform_delete():
try:
index = int(index_entry.get())
# Get all credentials to find the one with the given index
credentials = get_credentials()
if not credentials:
messagebox.showinfo("No Credentials", "No credentials to delete.")
return
if index < 1 or index > len(credentials):
raise IndexError("Index out of range")
# Get the credential at the specified index (1-based to 0-based)
cred = credentials[index - 1]
website = cred['website']
username = cred['username']
confirm = messagebox.askyesno("Confirm Deletion",
f"Are you sure you want to delete:\n\nWebsite: {website}\nUsername: {username}")
if confirm:
# Delete the credential using its database ID
delete_credential(cred['id'])
messagebox.showinfo("Success", "Credential deleted successfully.")
delete_window.destroy()
except ValueError:
messagebox.showerror("Error", "Invalid input. Enter a numeric index.")
except IndexError:
messagebox.showerror("Error", "Invalid index. Please select a valid credential.")
except Exception as e:
messagebox.showerror("Error", f"An unexpected error occurred: {e}")
# Button frame
button_frame = tk.Frame(content_frame, bg=BG_COLOR)
button_frame.pack(fill=tk.X, padx=PADDING, pady=10)
# Delete button
delete_button = create_button(button_frame, "Delete Credential", perform_delete, is_primary=True, is_danger=True)
delete_button.pack(pady=10)
# View credentials button to help user find the index - FIX: Make it consistent width
view_button = create_button(button_frame, "View Credentials", view_credentials)
view_button.pack(pady=5)
# Cancel button
cancel_button = create_button(button_frame, "Cancel", delete_window.destroy, is_primary=False)
cancel_button.pack(pady=5)
# Add credits
add_credits(delete_window)
# Initialize the database when the module is imported
initialize_database()