How to Create a random password generator in a separate window

Joined
May 26, 2022
Messages
2
Reaction score
0
Hi there, beginner to programming here.
I'm currently working on an assessment which requires me to code a simple login system using python. I've managed to do most of it myself and works well, but one feature I need and can't figure out is a password generator button in the menu. I've already got the button on the menu of my GUI, and once clicked it opens a window where I need to code the password generator into. The password generator needs to allow the user to choose how many characters they want, and whether they want/don't want to use normal characters, integers and/or special characters. Any help would be so appreciated. Here is my code so far:

from tkinter import *
import os
import random
# Creating the main menu window.
def main_menu():
# Making the main_window variable global so it can be used more widely.
global main_window
main_window = Tk()
# Defining the dimensions and title of the main menu window.
main_window.geometry("400x400")
main_window.title("Login System")
# Label that displays "Select an option" on the main menu.
LabelNum1 = Label(main_window,text="Select an option" , bg="white" , fg="red")
LabelNum1.pack(fill=X, pady=20)
# This is the properties for the first Login button.
loginButtonMain = Button(main_window,text="Login",width="30", height="2",command=login)
loginButtonMain.pack(pady=20)
# This is the properties for the first register button.
regButtonMain = Button(main_window,text="Register",width="30", height="2", command=register)
regButtonMain.pack(pady=20)

rndPassBtn = Button(main_window,text="Random Password Generator",width="30", height="2", command=generator)
rndPassBtn.pack(pady=20)
# This is a loop that stops code from running when a button is pressed until the window is closed.
main_window.mainloop()
# Defining the properties for the register command (so the Register button can function.)
def register():
global username
global password
global username_input
global password_input
global register_window

register_window=Toplevel(main_window)
register_window.geometry("400x400")
register_window.title("Register a New Account")
# Creating the username and password variables.
username= StringVar()
password= StringVar()
regLabel = Label(register_window,text="Please enter a username and password", bg="white", fg="red")
regLabel.pack(fill=X, pady=20)

#Creating a new frame within the window for more precise allocation of labels.

info_panel= Frame(register_window)
info_panel.pack(pady=20)

username_label=Label(info_panel,text="Username: ")
username_label.grid(row=0,column=0)
username_input= Entry(info_panel, textvariable=username)
username_input.grid(row=0,column=1)
#This is an empty label to provide space between username and password entry boxes.
Label(info_panel, text="").grid(row=1)

password_label=Label(info_panel,text="Password: ")
password_label.grid(row=2,column=0)
password_input= Entry(info_panel, textvariable=password)
password_input.grid(row=2,column=1)

regButton = Button(register_window, text="Register", command=regUser)
regButton.pack()
def regUser():
registered = False
username_txt=username.get()
password_txt=password.get()
file = open("accounts.txt","a")
for line in open("accounts.txt","r").readlines():
login_info = line.split()
if username_txt == login_info[1]:
registered = True
if registered:
file.close()
username_input.delete(0,END)
password_input.delete(0,END)
print("This user already exists")
failed_register_window = Toplevel(register_window)
failed_register_window.geometry("300x300")
failed_register_window.title("Failed to Register")
Label(failed_register_window, text="User already exists!",bg="white" , fg="red").pack(fill=X,pady=20)
okButton = Button(failed_register_window, text="OK", width="20" ,command = lambda :failed_register_window.destroy())
okButton.pack(pady=20)

else:
file.write("Username: "+username_txt+" Password: "+password_txt+"\n")
file.close()
username_input.delete(0,END)
password_input.delete(0,END)
successful_register_window = Toplevel(register_window)
successful_register_window.geometry("300x300")
successful_register_window.title("Successfully Registered!")
Label(successful_register_window, text="Successfully Registered!",bg="white" , fg="red").pack(fill=X,pady=20)
okButton = Button(successful_register_window, text="OK", width="20" ,command = lambda :successful_register_window.destroy())
okButton.pack(pady=20)

def login():
global username_verify
global username_verify_input
global password_verify_input
global password_verify
global login_window
login_window = Toplevel(main_window)
login_window.geometry("400x400")
login_window.title("Login")
loginLabel = Label(login_window,text="Please enter your login details below", bg="white" , fg="red")
loginLabel.pack(fill=X, pady=20)

login_panel= Frame(login_window)
login_panel.pack(pady=20)

username_verify=StringVar()
password_verify=StringVar()

username_label=Label(login_panel,text="Username: ")
username_label.grid(row=0,column=0)
username_verify_input = Entry(login_panel, textvariable=username_verify)
username_verify_input.grid(row=0,column=1)
# Empty Label to create space.
Label(login_panel, text="").grid(row=1)

password_label=Label(login_panel,text="Password: ")
password_label.grid(row=2,column=0)
password_verify_input = Entry(login_panel, textvariable=password_verify, show="*")
password_verify_input.grid(row=2,column=1)

loginButton = Button(login_window, text="Login", command = login_verify)
loginButton.pack(pady=20)
def login_verify():
user= username_verify.get()
passw= password_verify.get()
login= False
for line in open("accounts.txt","r").readlines():
login_info = line.split()
if user == login_info[1] and passw == login_info[3]:
login = True
if login:
LabelNum1 = Label(login_window,text="Successful Login" ,fg="green")
LabelNum1.pack()
else:
failed_login_window = Toplevel(login_window)
failed_login_window.geometry("200x200")
failed_login_window.title("Failed Login")
Label(failed_login_window, text="Failed to login",bg="white" , fg="red").pack(fill=X,pady=20)
okButton = Button(failed_login_window, text="OK", width ="20" ,command = lambda :failed_login_window.destroy())
okButton.pack(pady=20)

def generator():
generator_window = Toplevel(main_window)
generator_window.geometry("400x400")
generator_window.title("Password Generator")
generator_window_label = Label(generator_window,text="Please enter your desired properties for a randomly generated password", bg="white" , fg="red")
generator_window_label.pack(fill=X,pady=20)


main_menu()
 
Joined
May 26, 2022
Messages
2
Reaction score
0
Hey there, sorry I'm quite new to programming. How would I use code brackets? I just copy pasted the code entirely. The code that I've made so far does work and isn't the problem, I'm just trying to implement a random password generator into my code, that would be in the 'generator_window' as shown at the very bottom of the code
 
Joined
May 11, 2022
Messages
61
Reaction score
6
do you see the </> button? highlight your code then press it, check preview to see the result
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top