Skip to content

Commit

Permalink
Merge pull request #7 from MartinMerkli/main
Browse files Browse the repository at this point in the history
Refactor code and remove __pycache__
  • Loading branch information
IceTheCoder authored Jan 21, 2023
2 parents 9f21ec0 + fcd9084 commit 466048b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Byte-compiled python files
__pycache__/
*.py[cod]
Binary file removed PasswordGenerator/__pycache__/gui.cpython-310.pyc
Binary file not shown.
Binary file removed PasswordGenerator/__pycache__/logic.cpython-310.pyc
Binary file not shown.
Binary file removed PasswordGenerator/__pycache__/main.cpython-310.pyc
Binary file not shown.
12 changes: 7 additions & 5 deletions PasswordGenerator/logic.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import secrets
import string

characters = string.ascii_letters + string.punctuation + string.digits
CHARACTERS = string.ascii_letters + string.punctuation + string.digits

def generatePassword(requested_length) -> str:
'''

def generate_password(requested_length) -> str:
"""
Called by the CreatePasswordLabels function,
this function checks if the requested_length is valid,
returns a password if it is,
raises a value error if it's not.
:param int requested_length: The length inputed by the user in the input box.
:raises ValueError: if the requested_length is not an integer.
'''
"""

if requested_length > 0:
return ''.join(secrets.choice(characters) for i in range(min(int(requested_length), 100))) # This is where the password itself is generated
# This is where the password itself is generated
return ''.join(secrets.choice(CHARACTERS) for _ in range(min(int(requested_length), 100)))
else:
raise ValueError
136 changes: 71 additions & 65 deletions PasswordGenerator/main.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,94 @@
import tkinter as tk
import logic


def main():
window = tk.Tk()

WINDOW_WIDTH = 854
WINDOW_HEIGHT = 350
RESIZABLE_ABILITY = 0
APP_NAME = 'Passwordsy'
DESCRIPTION_FONT = 'Helvetica 12'
window_width = 854
window_height = 350
resizable_ability = False
app_name = 'Passwordsy'
description_font = 'Helvetica 12'

window.minsize(WINDOW_WIDTH, WINDOW_HEIGHT)
window.maxsize(WINDOW_WIDTH, WINDOW_HEIGHT)
window.grid_columnconfigure(0, weight = 1)
window.resizable(RESIZABLE_ABILITY, RESIZABLE_ABILITY)
window.minsize(window_width, window_height)
window.maxsize(window_width, window_height)
window.grid_columnconfigure(0, weight=1)
window.resizable(resizable_ability, resizable_ability)
window.iconphoto(False, tk.PhotoImage(file='logo.png'))
window.title(APP_NAME)

title = tk.Label(window, text = APP_NAME, font = 'Helvetica 24')
title.grid(row = 1)

question = tk.Label(window, text = 'Number of characters (up to 100):', font = DESCRIPTION_FONT)
question.grid(row = 3)

tip = tk.Label(window, text = 'CTRL + C to copy \nCTRL + V to paste', font = DESCRIPTION_FONT)
tip.grid(row = 2)

def createPasswordLabels(event) -> None:
'''
Called upon clicking the done button or pressing the ENTER key,
this function creates the password or error label,
and calls the ShowPassword function to show the passwords or error.
:param event: Used for calling the function when pressing the ENTER key.
'''

PASSWORD_WIDTH = 100
PASSWORD_HEIGHT = 1
PASSWORD_BORDER_WIDTH = 0
PASSWORD_FONT = 'Consolas 11'

error = 'An error occured. Try again with a whole number greater than 0.'

password_label_1 = tk.Text(window, width = PASSWORD_WIDTH, height = PASSWORD_HEIGHT, borderwidth = PASSWORD_BORDER_WIDTH, font = PASSWORD_FONT)
password_label_2 = tk.Text(window, width = PASSWORD_WIDTH, height = PASSWORD_HEIGHT, borderwidth = PASSWORD_BORDER_WIDTH, font = PASSWORD_FONT)
password_label_3 = tk.Text(window, width = PASSWORD_WIDTH, height = PASSWORD_HEIGHT, borderwidth = PASSWORD_BORDER_WIDTH, font = PASSWORD_FONT)
password_label_4 = tk.Text(window, width = PASSWORD_WIDTH, height = PASSWORD_HEIGHT, borderwidth = PASSWORD_BORDER_WIDTH, font = PASSWORD_FONT)
password_labels = [password_label_1, password_label_2, password_label_3, password_label_4]

try:
for password_label in password_labels:
password = logic.generatePassword(int(input_box.get()))
showPassword(password_label, password, password_labels.index(password_label))

except ValueError:
input_box.delete(0, 'end')
showPassword(password_label_1, error, 0)

input_box = tk.Entry(window, width = 10, borderwidth = 2)
input_box.bind("<Return>", createPasswordLabels)
input_box.grid(row = 4)

def showPassword(label, text, index) -> None:
'''
window.title(app_name)

title = tk.Label(window, text=app_name, font='Helvetica 24')
title.grid(row=1)

question = tk.Label(window, text='Number of characters (up to 100):', font=description_font)
question.grid(row=3)

tip = tk.Label(window, text='CTRL + C to copy \nCTRL + V to paste', font=description_font)
tip.grid(row=2)

def create_password_labels(_) -> None:
"""
Called upon clicking the done button or pressing the ENTER key,
this function creates the password or error label,
and calls the ShowPassword function to show the passwords or error.
:param _: Used for calling the function when pressing the ENTER key.
"""

password_width = 100
password_height = 1
password_border_width = 0
password_font = 'Consolas 11'

error = 'An error occured. Try again with a whole number greater than 0.'

password_label_1 = tk.Text(window, width=password_width, height=password_height,
borderwidth=password_border_width, font=password_font)
password_label_2 = tk.Text(window, width=password_width, height=password_height,
borderwidth=password_border_width, font=password_font)
password_label_3 = tk.Text(window, width=password_width, height=password_height,
borderwidth=password_border_width, font=password_font)
password_label_4 = tk.Text(window, width=password_width, height=password_height,
borderwidth=password_border_width, font=password_font)
password_labels = [password_label_1, password_label_2, password_label_3, password_label_4]

try:
for password_label in password_labels:
password = logic.generate_password(int(input_box.get()))
show_password(password_label, password, password_labels.index(password_label))

except ValueError:
input_box.delete(0, 'end')
show_password(password_label_1, error, 0)

input_box = tk.Entry(window, width=10, borderwidth=2)
input_box.bind("<Return>", create_password_labels)
input_box.grid(row=4)

def show_password(label, text, index) -> None:
"""
Called by the CreatePasswordLabels function,
this function displays the passwords or an error.
:param tk.Text label: Each individual password or error label.
:param str text: The generated password or the error.
:param int index: Which label is being displayed.
'''
"""
label.insert(1.0, text)
label.grid(row = 6 + index, pady = 5)
label.configure(state = 'disabled') # Makes the text uneditable.

label.grid(row=6 + index, pady=5)
label.configure(state='disabled') # Makes the text uneditable.

DONE_BUTTON_IMAGE = tk.PhotoImage(file = 'doneButton.png')
done_button_image = tk.PhotoImage(file='doneButton.png')

DONE_BUTTON_BORDER_WIDTH = 0
done_button_border_width = 0

done_btn = tk.Button(window, image = DONE_BUTTON_IMAGE, borderwidth = DONE_BUTTON_BORDER_WIDTH, command = lambda: createPasswordLabels(None))
done_btn.grid(row = 5, column = 0, pady = 10)
done_btn = tk.Button(window, image=done_button_image, borderwidth=done_button_border_width,
command=lambda: create_password_labels(None))
done_btn.grid(row=5, column=0, pady=10)

window.mainloop()


if __name__ == "__main__":
exit(main())

0 comments on commit 466048b

Please sign in to comment.