Sto riscontrando alcuni problemi con le istruzioni try ed except, ho un widget entry che accetta input in stringhe ma ho codice che lo converte in un intero successivo, il problema è se l'input dell'utente qualcosa come testo getta un errore come questo:variabile locale referenziata prima dell'assegnazione con try e ad eccezione dell'istruzione
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
return self.func(*args)
File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 157, in buttonclick_gamescreen
entryx = int(e1.get())
ValueError: invalid literal for int() with base 10: 'abc'
così ho voluto nascondere l'errore con l'istruzioni try ed except ma io ora ottenere un altro messaggio di errore.
Ecco come appare nel codice.
while pressed == 8 :
try:
entryx = int(e1.get())
entryy = int(e2.get())
except ValueError:
print("text")
answerx = answerlistx[randomimage]
answery = answerlisty[randomimage]
if entryx == answerx and entryy == answery
canvas.delete(images)
randomimage = random.randrange(0,49+1)
scorecounter = scorecounter + 1
game = PhotoImage(file=imagelist[randomimage])
images = canvas.create_image(30, 65, image = game, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 :
wrong = canvas.create_image(30, 65, image = outside, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
else:
wrong = canvas.create_image(30, 65, image = incorrect, anchor = NW)
e1.delete(0, END)
e2.delete(0, END)
pressed = ''
Il nuovo messaggio di errore:
text
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
return self.func(*args)
File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 165, in buttonclick_gamescreen
if entryx == answerx and entryy == answery:
UnboundLocalError: local variable 'entryx' referenced before assignment
non riesco a capire perché questo sta accadendo e come risolvere il problema in modo che qualsiasi aiuto sarebbe apprezzato.
Grazie in anticipo.