Skip to content

Commit

Permalink
Big changes in the main code.
Browse files Browse the repository at this point in the history
Big changes in the main code, added copy directly clicking on the labels, optimization and bug fixes as well.
  • Loading branch information
Dessmondd authored Sep 26, 2023
1 parent 7d60618 commit 29aa869
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class PasswordGenerator:
def __init__(self):
self.app = tk.Tk()
self.app.title("Memorable Password Generator")
self.app.title("SafePass - Password Generator")
self.app.resizable(width=False, height=False)
self.style = ttk.Style()
self.style.theme_use("xpnative")
Expand All @@ -23,6 +23,8 @@ def __init__(self):
self.numbers_var = tk.BooleanVar()
self.special_chars_var = tk.BooleanVar(value=True)
self.test_button = tk.Button(text="Test")
self.passphrases = [] # store the passphrases separately
self.passphrase_labels = [] # store the labels separately
self.create_widgets()

def toggle_multiple_passphrases(self):
Expand All @@ -34,7 +36,7 @@ def toggle_multiple_passphrases(self):
self.num_passphrases_label.grid_forget()
self.num_passphrases_spinbox.grid_forget()
self.num_passphrases_spinbox.config(state="disabled")

def create_widgets(self):
num_words_label = ttk.Label(self.app, text="Number of words:")
num_words_label.grid(row=0, column=0, padx=10, pady=5, sticky="w")
Expand All @@ -60,12 +62,6 @@ def create_widgets(self):
generate_button = ttk.Button(self.app, text="Generate", command=self.generate_button_clicked)
generate_button.grid(row=4, column=0, columnspan=2, padx=10, pady=10)

self.passphrase_label = ttk.Label(self.app, text="", wraplength=300)
self.passphrase_label.grid(row=5, column=0, columnspan=2, padx=10, pady=5)

self.copy_button = ttk.Button(self.app, text="Copy", command=self.copy_to_clipboard, state=tk.DISABLED)
self.copy_button.grid(row=6, column=0, columnspan=2, padx=10, pady=5)

self.strength_label = ttk.Label(self.app, text="", wraplength=300)
self.strength_label.grid(row=7, column=0, columnspan=2, padx=10, pady=5)

Expand All @@ -76,8 +72,6 @@ def create_widgets(self):
self.feedback_crack_time.grid(row=9, column=0, columnspan=2, padx=10, pady=5)

self.toggle_multiple_passphrases()



def generate_passphrase(self, num_words, add_numbers, add_special_chars, min_digits=1, max_digits=4, capitalize_percentage=60, include_spaces=True):
words = [secrets.choice(wordlist).capitalize() for _ in range(num_words)]
Expand All @@ -103,7 +97,7 @@ def generate_passphrase(self, num_words, add_numbers, add_special_chars, min_dig
return passphrase

raise RuntimeError(f'Unable to generate a secure passphrase after attempts')

def generate_button_clicked(self):
num_words = self.num_words_entry.get()
if num_words is None or num_words == "":
Expand All @@ -125,9 +119,17 @@ def generate_button_clicked(self):

for i in range(num_passphrases):
passphrases.append(self.generate_passphrase(num_words, self.numbers_var.get(), self.special_chars_var.get(), include_spaces=self.include_spaces.get()))

self.passphrase_label.config(text="\n".join([f"Passphrase {i+1}: {passphrase}" for i, passphrase in enumerate(passphrases)]))
self.copy_button.config(state=tk.NORMAL)
#Destroy previous labels, so it does not overlap.
for label in self.passphrase_labels:
label.destroy()

self.passphrases = passphrases # store the passphrases
self.passphrase_labels = [] # clear the old labels
for i, passphrase in enumerate(passphrases):
label = ttk.Label(self.app, text=f"Passphrase {i+1}: {passphrase}", wraplength=300)
label.grid(row=5+i, column=0, columnspan=2, padx=10, pady=5)
label.bind("<Button-1>", self.copy_to_clipboard) # bind click event
self.passphrase_labels.append(label) # store the label

strength_result = test_password(passphrases[0])
self.strength_label.config(text=f"Password Strength: {strength_result['score']}/4")
Expand All @@ -137,13 +139,20 @@ def generate_button_clicked(self):
except ValueError as e:
messagebox.showerror("Error", f"An error occurred while generating the passphrase:\n{e}")

def copy_to_clipboard(self):
passphrase = self.passphrase_label.cget("text").split(": ")[1]
def copy_to_clipboard(self, event=None):
# get the index of the clicked label
label_index = self.passphrase_labels.index(event.widget)
# get the corresponding passphrase
passphrase = self.passphrases[label_index]
pyperclip.copy(passphrase)
threading.Timer(clear_duration, clear_clipboard).start()

# provide visual feedback
event.widget.config(foreground="green")
self.app.after(2000, lambda: event.widget.config(foreground="black")) # change color back after 2 seconds

def run(self):
self.app.mainloop()

if __name__ == "__main__":
PasswordGenerator().run()
PasswordGenerator().run()

0 comments on commit 29aa869

Please sign in to comment.