forked from fenyx-it-academy/Class8-Python-Module-Week3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer4.py
More file actions
54 lines (40 loc) · 1.86 KB
/
answer4.py
File metadata and controls
54 lines (40 loc) · 1.86 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
from tkinter import *
from tkinter import filedialog
import random
import string
def generate_passowrd():
"""
This function is used to generate random password
and display the password in tkinter screen
"""
try:
global user_entry
pass_len = int(user_entry.get()) # get password length from user
rand_num = random.randrange(0, 10) # random number from 0 to 9
lower_case = string.ascii_lowercase # random upper case letter
upper_case = string.ascii_uppercase # random upper case letter
special_char = string.punctuation # random special character
rand_pass = ''.join([lower_case,upper_case,special_char,str(rand_num)])
rand_pass = random.sample(rand_pass,pass_len) # generate the random password
password_label.config(text=rand_pass)
except ValueError:
password_label.config(text='ERROR: Please Enter a number')
# start screen
screen = Tk()
title = screen.title('Password generator')
canvas = Canvas(screen, width=500, height=500)
canvas.pack()
# declare labels and buttons
greeting_label = Label(screen, text='WELCOME TO PASSWORD GENERATOR ', font=('Times', 15))
user_entry = Entry(screen, text='password length', font=('Arial', 15))
username_label = Label(screen, text='Enter your password length: ', font=('Calibri', 15))
password_label = Label(screen, text= "", font=('Arial', 15))
btn = Button(screen, bg='dark blue', padx='22', pady='5', font=('Arial', 15)
, fg='light blue', text='Generate password',command= generate_passowrd)
# Place to the canvas
canvas.create_window(250, 100, window=greeting_label)
canvas.create_window(250, 170, window=username_label)
canvas.create_window(250, 220, window=user_entry)
canvas.create_window(250, 280, window=btn)
canvas.create_window(250, 325, window=password_label)
screen.mainloop()