1

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.

Prateek Jaiswal
  • 145
  • 2
  • 15

1 Answers1

2

If you put try/except block in get_value function the exception is caught properly:

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:
            def get_value(event):
                try:
                    self.entry = e1.get()
                    self.entry = int(self.entry)
                    print(self.entry)
                except ValueError as e:
                    print("\n\tPlease Enter only Numbers!!")

            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


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()
  • That is not the point. The point is why this try except is not working. – Shubham Sharma May 20 '19 at 10:38
  • i have figured it out but i was trying another way of directly raising the exception from the method if it didn't work out well – Chetan Vashisth May 20 '19 at 10:43
  • @Stanisław Wilczyński : Yes, it works perfectly fine. But any reason why the earlier one was not working fine ? because there might be another statements out side that method definition, that would require exception handling. – Prateek Jaiswal May 20 '19 at 10:51
  • 1
    I think the solution you are looking for is using `report_callback_exception` - check out [this question](https://stackoverflow.com/questions/4770993/how-can-i-make-silent-exceptions-louder-in-tkinter) – sjwilczynski May 20 '19 at 11:16