Home > Software design >  It prints only 1 letter
It prints only 1 letter

Time:09-28

I'm trying to do my first project with tkinter: it's a gui password generator (I had already done a non-gui passwd generator with python so I'm sure the code to generate it is okay) but I'm having trouble in returning the final password. It only gives me 1 letter. Here's my code:

import tkinter as tk
import random

root = tk.Tk()

canvas = tk.Canvas(root, width=600, height=350)
canvas.grid()

#Labels
label1 = tk.Label(root, text = "How many characters?")
label1.place(x = 30, y = 50)
label2 = tk.Label(root, text = "How many passwords?")
label2.place(x = 30, y = 100)

#Entry box
box1 = tk.Entry(width = 20)
box1.place(x = 200, y = 50)


def password_generator():
    characters = "abcdefghilmopqrstuvzxwjkyèàòìùABCDEFGHILMNOPQRSTUVZWJKYX1234567890!£$%&/()=?^*§ç°:_;€"
    pass_lenght = int(box1.get())
    num_pass = int(box2.get())
    for x in range(0, pass_lenght):
        empty_password = ""
        real_password = random.choice(characters)
        empty_password = empty_password   real_password
        final = empty_password
        box3 = tk.Entry(width = 50, text='')
        box3.place(x = 50, y = 250)
        box3.insert(0, final)
#Button
button = tk.Button(root, text = "Commit", command = password_generator)
button.place(x = 260, y = 150)


root.mainloop()

CodePudding user response:

The problem is that you redefine empty_password every loop, you need to move it out:

def password_generator():
    characters = "abcdefghilmopqrstuvzxwjkyèàòìùABCDEFGHILMNOPQRSTUVZWJKYX1234567890!£$%&/()=?^*§ç°:_;€"
    pass_lenght = int(box1.get())
    num_pass = int(box2.get())
    empty_password = ""
    for x in range(0, pass_lenght):
        real_password = random.choice(characters)
        empty_password = empty_password   real_password
        final = empty_password
        box3 = tk.Entry(width = 50, text='')
        box3.place(x = 50, y = 250)
        box3.insert(0, final)

CodePudding user response:

It seems this issue arises from what is or is not included in your for loop. See a solution I tested below:

import tkinter as tk
import random

root = tk.Tk()

canvas = tk.Canvas(root, width=600, height=350)
canvas.grid()

#Labels
label1 = tk.Label(root, text = "How many characters?")
label1.place(x = 30, y = 50)
label2 = tk.Label(root, text = "How many passwords?")
label2.place(x = 30, y = 100)

#Entry box
box1 = tk.Entry(width = 20)
box1.place(x = 200, y = 50)

def password_generator():
    characters = "abcdefghilmopqrstuvzxwjkyèàòìùABCDEFGHILMNOPQRSTUVZWJKYX1234567890!£$%&/()=?^*§ç°:_;€"
    pass_lenght = int(box1.get())

    empty_password = ""  # Needs to be initialized BEFORE the loop, otherwise the value resets each time
    for x in range(0, pass_lenght):
        real_password = random.choice(characters)
        empty_password = empty_password   real_password
        final = empty_password

    # Add the final password to the box after the loop to generate the password has been completed
    box3 = tk.Entry(width = 50, text='')
    box3.place(x = 50, y = 250)
    box3.insert(0, final)

#Button
button = tk.Button(root, text = "Commit", command = password_generator)
button.place(x = 260, y = 150)


root.mainloop()

Briefly, the variable you initialized needs to come before the for loop, so it's not reset every time, and you need to add the string to the entry box beneath the for loop, so that the whole string is added.

However, this is not the most efficient way to generate a random string of letters. Inspired by this thread you could also try:

final = ''.join(random.choice(string.ascii_letters   string.punctuation) for i in range(pass_lenght))

Be sure to add:

import string

Check out more info on string. Your test case does not include an entry box for number of passwords. This answer does not address needing to generate multiple passwords.

  • Related