0

I am coding a password generator with an interface for school and I can't seem to find out where to put the password generator piece in my program.

import random
from tkinter import *

characters = "abcdefABCDEF1234!@#$"

length = 8

window = Tk()

window.title('Password Generator')

while True:
    input("Press Enter to generate new password")
    password = "".join(random.sample(characters, length))
    print(password)

label = Label (window, print(password))

label.pack(padx = 200, pady = 50)

window.mainloop()
4
  • Are you asking where to put the label or where to put the code that generates the password? Or both? Whatever you're asking is fine, I just need clarity so I can try my best to answer. Commented Apr 17, 2021 at 2:03
  • I am asking where about where to put the code that generates the password Commented Apr 17, 2021 at 2:14
  • Np. What does the end result have to be? Something that works? Is there a requirement to use a function or something? Commented Apr 17, 2021 at 2:24
  • the end result needs to be a working password generator with a GUI and whenever you press Enter it generated a new password. Commented Apr 17, 2021 at 12:52

1 Answer 1

2

It's hard to understand what exactly you are trying to achieve. Since it is a password generator, based on your previous code and my assumption, I have made some changes to your code. It generates and displays a new password on every button click.

import random
from tkinter import *

characters = "abcdefABCDEF1234!@#$"

length = 8

def generatepassword():
    password = "".join(random.sample(characters, length))
    label.config(text=password)
    
window = Tk()

window.title('Password Generator')

generatebtn = Button(window,text="Click to Generate Password",command=generatepassword)
generatebtn.pack()

label = Label (window,text="")

label.pack(padx = 200, pady = 50)

window.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.