Reputation: 11
When I try to generate a password, even if you enter the length Python says that the variable lengthstr
is empty.
This is the error that Python gives me:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/salvatore/Python Projects/Password Saver/Ps.py", line 35, in <lambda>
generatebutton = tk.Button(window, text="Genera", font=("Helvetica", 12, "bold"), bg="#007acc", activebackground="#66c2ff", command=lambda: generate(int(lengthstr)))
ValueError: invalid literal for int() with base 10: ''
Here is my code:
def generate(length):
return length
chars = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&£*()?"
passgen = "".join(choice(chars) for x in range(length))
passwd = tk.Entry(window)
passwd.insert(0, passgen)
passwd.grid(row=5, column=0, sticky="N")
def generatepassword():
generatefunction.destroy()
seepassbutton.destroy()
text.destroy()
textgen = "Genera una Password"
textgen = tk.Label(window, text=textgen, fg="#333333", font=("Helvetica", 32, "bold"))
textgen.grid(row=0, column=0, sticky="N")
length = tk.Entry(window, justify="center")
length.grid(row=2, column=0, pady=10, ipadx=42)
lengthstr = length.get()
print(lengthstr)
textgen = "Inserisci la lunghezza della Password"
textgen = tk.Label(window, text=textgen, fg="#333333", font=("Helvetica", 10))
textgen.grid(row=3, column=0, sticky="N")
generatebutton = tk.Button(window, text="Genera", font=("Helvetica", 12, "bold"), bg="#007acc", activebackground="#66c2ff", command=lambda: generate(int(lengthstr)))
generatebutton.config(relief="flat")
generatebutton.grid(row=4, column=0, pady=6, ipady=1, ipadx=240)
Upvotes: 0
Views: 89
Reputation: 359
def generate(length):
#This `return length` won't let your logic in this function run
return length
chars = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&£*()?"
passgen = "".join(choice(chars) for x in range(length))
passwd = tk.Entry(window)
passwd.insert(0, passgen)
passwd.grid(row=5, column=0, sticky="N")
But the error is from command of your button
generatebutton = tk.Button(window, ...., command=lambda: generate(int(lengthstr)))
You are trying to convert an invalid literal to an int, what is the value of lengthstr? if it is a float literal e.g '1.5' you have to convert to float before converting to int like so:
generatebutton = tk.Button(window, ..., command=lambda:generate(int(float(lengthstr)))
I found your lengthstr
variable, at the time where you did this lengthstr = length.get()
the entry will be ''
which is an empty string and can't be converted to an Int..
Move lengthstr = length.get()
into the generate function so your Button only gets the Entry field once the user clicks the button, and then you can convert to an int..
Remove the function parameter, then in the command, just call the function.
Upvotes: 2
Reputation: 1384
To avoid this error, you need to call .get()
ONLY when the user has entered in the text; for this purpose I suggest using a Button:
def getTextFromEntry(theEntry):
global lengthstr
lengthstr = length.get()
print(lengthstr)
def generatepassword():
generatefunction.destroy()
seepassbutton.destroy()
text.destroy()
textgen = "Genera una Password"
textgen = tk.Label(window, text=textgen, fg="#333333", font=("Helvetica", 32, "bold"))
textgen.grid(row=0, column=0, sticky="N")
length = tk.Entry(window, justify="center")
length.grid(row=2, column=0, pady=10, ipadx=42)
getValue = Button(text="Generate password", command=getTextFromEntry)
getValue.pack()
textgen = "Inserisci la lunghezza della Password"
textgen = tk.Label(window, text=textgen, fg="#333333", font=("Helvetica", 10))
textgen.grid(row=3, column=0, sticky="N")
generatebutton = tk.Button(window, text="Genera", font=("Helvetica", 12, "bold"), bg="#007acc", activebackground="#66c2ff", command=lambda: generate(int(lengthstr)))
generatebutton.config(relief="flat")
generatebutton.grid(row=4, column=0, pady=6, ipady=1, ipadx=240)
(I'm sure there's an easier way to do this using lambda
s, but this is the best I can do ;)
You can also try using try
and except
statements (like @10Rep's answer but with getting the value (.get()
) and the conversion to int
in a loop, and stopping the loop when successful.
Upvotes: 0
Reputation: 2270
The reason you are getting this error is likely because you have entered nothing into the Entry()
. To avoid this error, use a try
and except
.
This would be the creation of the button:
try:
generatebutton = tk.Button(window, text="Genera", font=("Helvetica", 12, "bold"), bg="#007acc", activebackground="#66c2ff", command=lambda: generate(int(lengthstr)))
except (ValueError, NameError):
pass
generatebutton.config(relief="flat")
generatebutton.grid(row=4, column=0, pady=6, ipady=1, ipadx=240)
Upvotes: 0