100 Days Of Python - Day 29
Day 29
Standard Dialogs
Standard Dialogs
are used to get input from the user.- They are similar to
alerts
andprompts
inJavaScript
. - The
tkinter
module has a number of standard dialogs that you can use. - These are pop-up windows that appear on top of the current window, and are used to get input from the user or to display information.
- The
tkinter
module has the following standard dialogs −tkinter.messagebox
tkinter.filedialog
tkinter.colorchooser
tkinter.font
tkinter.scrolledtext
tkinter.simpledialog
Password Generator
- The
password generator
is a simple program that generates a random password. - The user inputs a website name, an email/username, and a password.
- The user can also generate a random password which is copied to the clipboard.
- When the user clicks the
Add
button, the website name, email/username, and password are added to a text file which is stored in the same directory as the program.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from tkinter import *
from tkinter import messagebox
from random import choice, randint, shuffle
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
#Password Generator Project
import random
import pyperclip
#Password Generator Project
# def generate_password():
# #Pass
# password_entry.delete(0, END)
# #Password list
# password_list = []
# #Letters
# for char in range(random.randint(8, 10)):
# password_list.append(chr(random.randint(97, 122)))
# #Symbols
# for char in range(random.randint(2, 4)):
# password_list += chr(random.randint(33, 47))
# #Numbers
# for char in range(random.randint(2, 4)):
# password_list += chr(random.randint(48, 57))
# #Shuffle
# random.shuffle(password_list)
# #Password
# password = ""
# for char in password_list:
# password += char
# #Output
# password_entry.insert(0, password)
# pyperclip.copy(password)
# OR we could use list comprehension
def generate_password():
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
symbols = ["!", "#", "$", "%", "&", "(", ")", "*", "+"]
password_letters = [choice(letters) for _ in range(randint(8, 10))]
password_symbols = [choice(symbols) for _ in range(randint(2, 4))]
password_numbers = [choice(numbers) for _ in range(randint(2, 4))]
password_list = password_letters + password_symbols + password_numbers
shuffle(password_list)
password = "".join(password_list)
password_entry.insert(0, password)
pyperclip.copy(password)
# ---------------------------- SEARCH PASSWORD ------------------------------- #
# get the data from the entries
# search for the website in the file
# if found, show the email and password
# if not found, show an error message
# def search():
# website = website_entry.get()
# try:
# with open("data.txt") as data_file:
# for line in data_file:
# if website in line:
# email, password = line.split(" | ")[1:3]
# messagebox.showinfo(title=website, message=f"Email: {email}\nPassword: {password}")
# except FileNotFoundError:
# messagebox.showinfo(title="Error", message="No Data File Found")
# except ValueError:
# messagebox.showinfo(title="Error", message="No details for the website exists")
# ---------------------------- SAVE PASSWORD ------------------------------- #
# get the data from the entries
# create a dictionary
# add the data to the dictionary
# clear the entries
# save the data to a file
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
if len(website) == 0 or len(password) == 0:
messagebox.showinfo(title="Oops", message="Please don't leave any fields empty")
else:
is_ok = messagebox.askokcancel(title=website, message=f"These are the details entered: \nEmail: {email}\n"
f"Password: {password}\nIs it ok to save?")
if is_ok:
with open("data.txt", "a") as data_file:
data_file.write(f"{website} | {email} | {password}\n")
website_entry.delete(0, END)
password_entry.delete(0, END)
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Password Manager")
window.config(padx=50, pady=50)
canvas = Canvas(width=200, height=200)
logo_img = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=logo_img)
canvas.grid(row=0, column=1)
# Labels
website_label = Label(text="Website:")
website_label.grid(row=1, column=0)
email_label = Label(text="Email/Username:")
email_label.grid(row=2, column=0)
password_label = Label(text="Password:")
password_label.grid(row=3, column=0)
# Entries
# wesite
website_entry = Entry(width=44)
website_entry.grid(row=1, column=1, columnspan=2)
website_entry.focus()# Cursor starts here
# email
email_entry = Entry(width=44)
email_entry.grid(row=2, column=1, columnspan=2)
email_entry.insert(0, "johndoe@gmail.com")# Default value
# password
password_entry = Entry(width=24)
password_entry.grid(row=3, column=1)
# Buttons
generate_password_button = Button(text="Generate Password", command=generate_password)
generate_password_button.grid(row=3, column=2)
add_button = Button(text="Add", width=41, command=save)
add_button.grid(row=4, column=1, columnspan=2)
window.mainloop()
This post is licensed under CC BY 4.0 by the author.