I have written a simple python code, to Input any number from the user. In case anything else is entered an exception is raised as per the code. I typecast the entered value in into int just to check whether it is an integer. Ideally i am excepting that when i enter any alphabet, the exception should be raised and should print the text that i have given. But still i can see that its not being caught.
However, when i specifically add one more try-except block around the typecast statement, it works.
from tkinter import *
window = Tk()
window.geometry('400x400')
class hangman:
def __init__(self):
self.entry = ''
def clicked(self, label2):
label2.place(x=100, y=200)
while True:
try:
def get_value(event):
self.entry = e1.get()
self.entry = int(self.entry)
print(self.entry)
Label(window, text="Enter any number :").place(x=10, y=220)
e1 = Entry(window)
e1.place(x=10, y=240)
e1.bind('<Return>', get_value) #To get the value entered in the entry when Return is pressed.
print("Past bind1")
print(self.entry)
print("Past bind2")
break
except ValueError as e :
print("\n\tPlease Enter only Numbers!!")
obj1 = hangman()
label2 = Label(window, text="Start")
bt = Button(window, text="Play", command=lambda: obj1.clicked(label2))
bt.place(x=150, y=125)
window.mainloop()
I expect the exception to be caught and the print my message instead of the standard exception error.