My Sample Code:
from tkinter import *
class first:
def __init__(self):
self.top = Tk()
...
def test(self):
try:
self.value = self.dict[key]
except KeyError:
try:
second()
except ValueError:
print('Finally')
class second:
def __init__(self):
self.frame = Toplevel()
...
self.button = ttk.Button(parent=self.frame, text='GO', command=self.go_click)
...
def go_click(self):
raise ValueError('Not Valid')
That´s just an example! The problem is that the ValueError is raised by the second class, but it is not handled by the except clause of the first class. Below the traceback:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
return self.func(*args)
File "........", line xxx, in goclick
raise ValueError('Not Valid')
ValueError: Not Valid
How can I properly handle it?
Thanks,
try this
from tkinter import *
class first:
def __init__(self):
self.top = Tk()
...
def test(self):
try:
self.value = self.dict[key]
except KeyError:
try:
second()
except ValueError:
print('Finally')
print "OK CALLED SECOND()!!!!" #######THIS PRINT MEANS YOUR DONE HERE
class second:
def __init__(self):
self.frame = Toplevel()
...
self.button = ttk.Button(parent=self.frame, text='GO', command=self.go_click)
...
def go_click(self):
raise ValueError('Not Valid')
in order to actually handle that error you would need to override tkinters event loop ... not very easy(or good practice in general)
a better way to do it would be to handle the error in the go_click
function its self something like
def go_click(self):
try:
self.submit_form()
except ValueError:
print "VALIDATION ERROR!!"