How To Handle Errors In Tkinter Mainloop?
I have a python program which is scraping web data for a client. tkinter is used for the interface. Outline is: Window 1 lets the user select what information to scrape. Window 1
Solution 1:
To handle tkinter errors you do the following
classTkErrorCatcher:
'''
In some cases tkinter will only print the traceback.
Enables the program to catch tkinter errors normally
To use
import tkinter
tkinter.CallWrapper = TkErrorCatcher
'''def__init__(self, func, subst, widget):
self.func = func
self.subst = subst
self.widget = widget
def__call__(self, *args):
try:
if self.subst:
args = self.subst(*args)
return self.func(*args)
except SystemExit as msg:
raise SystemExit(msg)
except Exception as err:
raise err
import tkinter
tkinter.CallWrapper = TkErrorCatcher
But in your case please do not do this. This should only ever be done in the case of wanting to hide error messages from your users in production time. As commented above you have a nono's going on.
To spawn multiple windows you can use tkinter.Toplevel
I would recommend in general to read
- http://www.tkdocs.com/tutorial/index.html
- http://effbot.org/tkinterbook/tkinter-hello-tkinter.htm
- http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
and for your specific problem of threading in tkinter this blog post nails it. Basically you need to have the tkinter mainloop blocking the programs main thread, then use after calls from other threads to run other code in the mainloop.
Post a Comment for "How To Handle Errors In Tkinter Mainloop?"