11import tkinter as tk
22
3- import generate_password_logic
3+ import generate_password_logic as logic
44
55title_font = 'Helvetica 24'
66section_title_font = 'Helvetica 16'
1515no_character_set_error = 'An error occurred. Try again with at least 1 character set.'
1616double_error = 'An error occurred. Try again with at least 1 character set and a whole number between 4 and 100.'
1717
18- def show_generate_password_frame (frame , done_btn_image ) -> None :
18+ def create_generate_password_frame (frame , done_btn_image ) -> None :
1919 '''
2020 Called upon starting the program,
2121 this function uses the Tkinter module to create a GUI frame to generate passwords with various options for customisation
22- (length and character sets)
22+ (length and character sets),
2323 and serves as a hub for all other password generation functions.
2424
2525 Parameters
@@ -38,8 +38,6 @@ def show_generate_password_frame(frame, done_btn_image) -> None:
3838 password_label_4 = tk .Text (frame , width = password_width , height = password_height ,
3939 borderwidth = password_border_width , font = password_font )
4040 password_labels = [password_label_1 , password_label_2 , password_label_3 , password_label_4 ]
41-
42- global copy_button
4341
4442 frame_title = tk .Label (frame , text = 'Generate password' , font = title_font )
4543 frame_title .grid (column = 0 , row = 1 , columnspan = 2 )
@@ -67,38 +65,47 @@ def show_generate_password_frame(frame, done_btn_image) -> None:
6765 punctuation_text = tk .Label (frame , text = 'Punctuation' , font = description_font )
6866
6967 checkboxes = [lowercase_letters_checkbox , uppercase_letters_checkbox , digits_checkbox , punctuation_checkbox ]
70- checkboxes_text = [lowercase_letters_text , uppercase_letters_text , digits_text , punctuation_text ]
68+ checkboxes_text_labels = [lowercase_letters_text , uppercase_letters_text , digits_text , punctuation_text ]
7169
7270 for checkbox in checkboxes :
7371 checkbox .grid (column = 1 , row = 5 + checkboxes .index (checkbox ), pady = 8 )
7472 checkbox .select ()
7573
76- for text in checkboxes_text :
77- text .grid (column = 2 , row = 5 + checkboxes_text .index (text ), sticky = 'w' )
74+ for text in checkboxes_text_labels :
75+ text .grid (column = 2 , row = 5 + checkboxes_text_labels .index (text ), sticky = 'w' )
7876
7977 def create_password_labels (event ) -> None :
8078 '''
8179 Called upon clicking the done button or pressing the ENTER key,
82- this function validates the user input.
83- If it is valid, this function uses the show_text function to display a password,
84- else it uses the same function to display an adequate error given through the validate_input function of generate_password_logic.py
80+ this function calls determine_error and validate_character_sets of generate_password_logic,
81+ and then settles whether an error has occurred or not.
82+ If an error has occurred, the function displays said error
83+ (obtained through determine_error),
84+ and displays it on the screen through show_text.
85+ If an error has not occurred, the function calls generate_password of generate_password_logic.py to get 4 passwords,
86+ and calls the show_text function to display them to the user.
8587
8688 Parameters
8789 ----------
8890 event:
8991 Necessary for initiating the function when pressing the ENTER key.
9092 '''
91- text = generate_password_logic . validate_input ( input_box . get (), lowercase_letters_var , uppercase_letters_var , digits_var , punctuation_var , no_character_set_error , input_box , double_error , invalid_input_error )
92-
93- # Check if an error is NOT returned.
94- if text == None :
93+ text = logic . determine_error ( logic . validate_character_sets ( lowercase_letters_var , uppercase_letters_var , digits_var , punctuation_var ),
94+ input_box . get (), no_character_set_error , double_error , invalid_input_error )
95+
96+ if text == '' :
9597 for password_label in password_labels :
96- password_label .bind ('<ButtonRelease>' , lambda event : generate_password_logic .show_copy_button (event , copy_button ))
98+ password_label .bind ('<ButtonRelease>' , lambda event : logic .show_copy_button (event , copy_button ))
99+
100+ adapted_input = logic .adapt_input (input_box .get ())
101+ input_box .delete (0 , 'end' )
102+ input_box .insert (1 , adapted_input )
97103
98- text = generate_password_logic .generate_password (input_box . get () , lowercase_letters_var , uppercase_letters_var , digits_var , punctuation_var )
104+ text = logic .generate_password (adapted_input , lowercase_letters_var , uppercase_letters_var , digits_var , punctuation_var )
99105 show_text (password_label , text )
100106 password_label .grid (column = 0 , row = 5 + password_labels .index (password_label ), pady = 10 , padx = 10 )
101107 else :
108+ input_box .delete (0 , 'end' )
102109 password_label_1 .grid (column = 0 , row = 5 , padx = 10 , pady = 10 )
103110 show_text (password_label_1 , text )
104111
@@ -111,13 +118,14 @@ def create_password_labels(event) -> None:
111118 done_btn .grid (column = 0 , row = 4 , columnspan = 2 )
112119
113120 copy_button = tk .Menu (frame , tearoff = False )
114- copy_button .add_command (label = 'Copy' , command = lambda : generate_password_logic .copy_text (input_box , password_labels ))
121+ copy_button .add_command (label = 'Copy' , command = lambda : logic .copy_text (input_box , password_labels ))
115122
116123 def show_text (label , text ) -> None :
117124 '''
118125 Called by the create_password_labels function,
119126 this function updates the contents of the password_labels,
120- by enabling the label, deleting its current contents, inserting the new text, and then disabling the label again.
127+ by enabling the label, deleting its current contents,
128+ inserting the new text, and then disabling the label again.
121129
122130 Parameters
123131 ----------
@@ -135,7 +143,7 @@ def show_text(label, text) -> None:
135143def select_input_box (event ) -> None :
136144 '''
137145 Called whenever the tab is changed,
138- this function focuses to the input box,
146+ this function focuses the keyboard to the input box,
139147 which allows the user to start typing immediately without having to click on the input box first.
140148 '''
141149 input_box .focus ()
0 commit comments