Skip to content

Commit

Permalink
oop for password and menu, working on window
Browse files Browse the repository at this point in the history
  • Loading branch information
mclacore committed Jul 22, 2024
1 parent 69970f1 commit d985348
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 137 deletions.
73 changes: 0 additions & 73 deletions main.py

This file was deleted.

55 changes: 28 additions & 27 deletions menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
from tkinter import messagebox, Menu


def create_menu(root):
"""Creates the menu bar"""
menubar = Menu(root)
create_file_bar(menubar, root)
create_help_bar(menubar)
return menubar


def create_file_bar(menubar, root):
"""Creates the file menu bar"""
file = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=file)
file.add_command(label="Close", command=root.destroy)
return file


def create_help_bar(menubar):
"""Creates the help menu bar"""
help_menu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=about)
return help


def about():
"""Displays custom about message"""
messagebox.showinfo("About", "This is a simple password generator app written by @mclacore.")
class MenuBar:
def create_menu(self, root):
"""Creates the menu bar"""
menubar = Menu(root)
self.create_file_bar(menubar, root)
self.create_help_bar(menubar)
return menubar


def create_file_bar(self, menubar, root):
"""Creates the file menu bar"""
file = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=file)
file.add_command(label="Close", command=root.destroy)
return file


def create_help_bar(self, menubar):
"""Creates the help menu bar"""
help_menu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=self.about)
return help


def about(self):
"""Displays custom about message"""
messagebox.showinfo("About", "This is a simple password generator app written by @mclacore.")
76 changes: 39 additions & 37 deletions password.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,42 @@
import pyperclip


def generate_password(length, numbers, symbols):
"""Generates the password based on options"""
if (symbols == 0) and (numbers == 0):
alphabet = string.ascii_letters
password = ''.join(secrets.choice(alphabet) for i in range(length))
elif (symbols == 1) and (numbers == 0):
alphabet = string.ascii_letters + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(length))
elif (symbols == 0) and (numbers == 1):
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(length))
else:
alphabet = string.ascii_letters + string.digits + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(length))
return password


def copy_password(password, copy_pass):
"""Copies the password to the clipboard."""
pyperclip.copy(password)
copy_pass['text'] = "Copied!"


def show_password(password, root, length, show_pass):
"""Shows the password in the text box."""
pass_box = Text(root, height=1, width=length.get() + 1)
pass_box.grid(column=2, row=8, sticky='w')
pass_box.insert(END, password)
show_pass['text'] = "Hide Password"
show_pass['command'] = lambda: hide_password(pass_box, root, length, password, show_pass)


def hide_password(pass_box, root, length, password, show_pass):
"""Hides the password in the text box"""
pass_box.grid_remove()
show_pass['text'] = "Show Password"
show_pass['command'] = lambda: show_password(password, root, length, show_pass)
class PasswordGenerator:
def generate_password(self, length, numbers, symbols):
"""Generates the password based on options"""
if (symbols == 0) and (numbers == 0):
alphabet = string.ascii_letters
password = ''.join(secrets.choice(alphabet) for i in range(length))
elif (symbols == 1) and (numbers == 0):
alphabet = string.ascii_letters + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(length))
elif (symbols == 0) and (numbers == 1):
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(length))
else:
alphabet = string.ascii_letters + string.digits + string.punctuation
password = ''.join(secrets.choice(alphabet) for i in range(length))
return password


def copy_password(self, password, copy_pass):
"""Copies the password to the clipboard."""
pyperclip.copy(password)
copy_pass['text'] = "Copied!"


def show_password(self, password, root, length, show_pass):
"""Shows the password in the text box."""
pass_box = Text(root, height=1, width=length.get() + 1)
pass_box.grid(column=2, row=8, sticky='w')
pass_box.insert(END, password)
show_pass['text'] = "Hide Password"
show_pass['command'] = lambda: self.hide_password(pass_box, root, length, password, show_pass)


def hide_password(self, pass_box, root, length, password, show_pass):
"""Hides the password in the text box"""
pass_box.grid_remove()
show_pass['text'] = "Show Password"
show_pass['command'] = lambda: self.show_password(password, root, length, show_pass)

80 changes: 80 additions & 0 deletions window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""Adds GUI to password generator"""
from tkinter import Tk, Label, Button, Scale, Checkbutton, IntVar, HORIZONTAL
from menu import MenuBar
from password import PasswordGenerator


class Window:
def __init__(self, root, title="Password Generator", geometry="300x300", password_generator=None, menu_bar=None):
self.root = root
self.root.title(title)
self.root.geometry(geometry)
self.password_generator = password_generator
self.menu_bar = menu_bar

self.init_menu()

self.title_label = Label(self.root, text="Generate a new password")
self.title_label.grid(column=1, row=1, sticky='w')

self.length = Scale(self.root, from_=8, to=128, orient=HORIZONTAL, label="Length", troughcolor="blue")
self.length.grid(column=1, row=3, sticky='w')

self.num_val = IntVar()
self.numbers = Checkbutton(self.root, text="Numbers", variable=self.num_val, onvalue=1, offvalue=0)
self.numbers.grid(column=1, row=4, sticky='w')

self.sym_val = IntVar()
self.symbols = Checkbutton(self.root, text="Symbols", variable=self.sym_val, onvalue=1, offvalue=0)
self.symbols.grid(column=1, row=5, sticky='w')

self.gen_pass = Button(self.root, text="Generate Password", fg="blue", command=self.pass_generated)
self.gen_pass.grid(column=1, row=6, sticky='w')


def init_menu(self):
if self.menu_bar:
menubar = self.menu_bar.create_menu(self.root)
self.root.config(menu=menubar)


def restart_app(self):
self.root.destroy()
self.__init__(root)
self.window.run()


def pass_generated(self):
password = self.password_generator.generate_password(self.length.get(), self.num_val.get(), self.sym_val.get())
self.title_label.configure(text="Password generated")
self.gen_pass.grid_remove()

copy_pass = Button(
self.root,
text="Copy Password",
fg="blue",
command=lambda: self.password_generator.copy_password(password, copy_pass))
copy_pass.grid(column=1, row=7, sticky='w')

show_pass = Button(
self.root,
text="Show Password",
fg="red",command=lambda: self.password_generator.show_password(password, self.root, self.length, show_pass))
show_pass.grid(column=1, row=8, sticky='w')

start_over = Button(
self.root,
text="Start Over",
fg="black",
command=self.restart_app)
start_over.grid(column=1, row=9, sticky='w')


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


if __name__ == "__main__":
root = Tk()
app = Window(root, password_generator=PasswordGenerator(), menu_bar=MenuBar())
app.run()

0 comments on commit d985348

Please sign in to comment.