-1

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 making the user's input an integer and I keep getting this error:

ValueError: invalid literal for int() with base 10: ''

Here's the 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)

box2 = tk.Entry(width = 20)
box2.place(x = 200, y = 100)


answer = tk.Label(root, text = "")
answer.place(x = 260, y = 200)

pass_lenght = int(box1.get())
num_pass = int(box2.get())

def password_generator():
    characters = "abcdefghilmopqrstuvzxwjkyèàòìùABCDEFGHILMNOPQRSTUVZWJKYX1234567890!£$%&/()=?^*§ç°:_;€"
    for x in range(0, pass_lenght):
        empty_password = ""
        for x in range(0, num_pass):
            real_password = random.choice(caratteri)
            empty_password    = empty_password + real_password
        print("Here's your password: ", empty_password)

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


root.mainloop()
cottontail
  • 10,268
  • 18
  • 50
  • 51
  • try adding a default value to you code, or simply forcing user to give input before proceeding to call `password_generator`, by adding `input validation`. – Me Bottle O Scrumpy Sep 11 '22 at 16:49
  • 2
    For starters you don't want `command = password_generator()` but rather `command=password_generator`. Also what is `caratteri` – Rory Sep 11 '22 at 16:49
  • Does this answer your question? [Why is my Button's command executed immediately when I create the Button, and not when I click it?](https://stackoverflow.com/questions/5767228/why-is-my-buttons-command-executed-immediately-when-i-create-the-button-and-no) – TheLizzard Sep 11 '22 at 16:56
  • I've tried to take out the brackets but unfortunately it didn't work. Regarding "caratteri" it's just a silly mistake since I'm italian and I've translated all the variables in order to make it understandable to everybody, though I forgot it. Btw caratteri means characters. – Cesare Vertolo Sep 11 '22 at 17:31
  • @CesareVertolo After removing the brackets, move `pass_lenght = int(box1.get())` and `num_pass = int(box2.get())` inside the function. – TheLizzard Sep 11 '22 at 17:33
  • @TheLizzard I'm still getting the same error EDIT: it is working now, thank you very much. – Cesare Vertolo Sep 11 '22 at 17:36

1 Answers1

1
  1. command should be without brackets ()
  2. makes no sense to call pass_lenght = int(box1.get()) before any input

.

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)

box2 = tk.Entry(width = 20)
box2.place(x = 200, y = 100)


answer = tk.Label(root, text = "")
answer.place(x = 260, y = 200)



def password_generator():
    pass_lenght = int(box1.get())
    num_pass = int(box2.get())
    characters = "abcdefghilmopqrstuvzxwjkyèàòìùABCDEFGHILMNOPQRSTUVZWJKYX1234567890!£$%&/()=?^*§ç°:_;€"
    for x in range(0, pass_lenght):
        empty_password = ""
        for x in range(0, num_pass):
            real_password = random.choice(characters)
            empty_password    = empty_password + real_password
        print("Here's your password: ", empty_password)
    

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


root.mainloop()
Module_art
  • 999
  • 2
  • 9
  • 26