Tkinter and exceptions

Discussion in 'Python' started by Nick Craig-Wood, Jul 26, 2006.

  1. I'm just starting out with Tkinter programming (using Programming
    Python as a reference), and I couldn't find the answer to this
    anywhere...

    How do you catch general exceptions in a Tkinter program. If you run
    the below and click the "Exception" or "Callback Exception" buttons
    you see a traceback on stderr under unix, and nothing at all under
    windows (if run as a pyw).

    How so you catch those exceptions so that they can pop up in a dialog?
    There doesn't seem to be a hook. I was imagining that there would be a
    global error handler I could hook / override?

    from Tkinter import *

    class AppDemo(Frame):
    """A sample tk app"""

    def __init__(self, parent=None):
    Frame.__init__(self, parent)
    self.pack(expand=YES, fill=BOTH)
    self.make_tool_bar()
    self.master.title("A Title")
    self.bang = 0
    self.process()

    def process(self):
    self.after(1000, self.process)
    if self.bang:
    self.bang = 0
    raise ValueError("Callback Exception")

    def make_tool_bar(self):
    self.toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
    self.toolbar.pack(side=BOTTOM, fill=X)
    Button(self.toolbar, text='Exception', command=self.make_exception).pack(side=TOP, fill=X)
    Button(self.toolbar, text='Callback Exception', command=self.make_callback_exception).pack(side=TOP, fill=X)
    Button(self.toolbar, text='Quit', command=self.quit).pack(side=TOP, fill=X)

    def make_exception(self):
    raise ValueError("Exception")

    def make_callback_exception(self):
    self.bang = 1

    if __name__ == "__main__":
    AppDemo().mainloop()

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
    Nick Craig-Wood, Jul 26, 2006
    #1
    1. Advertising

  2. Nick Craig-Wood

    Peter Otten Guest

    Nick Craig-Wood wrote:

    > I'm just starting out with Tkinter programming (using Programming
    > Python as a reference), and I couldn't find the answer to this
    > anywhere...
    >
    > How do you catch general exceptions in a Tkinter program. If you run
    > the below and click the "Exception" or "Callback Exception" buttons
    > you see a traceback on stderr under unix, and nothing at all under
    > windows (if run as a pyw).
    >
    > How so you catch those exceptions so that they can pop up in a dialog?
    > There doesn't seem to be a hook. I was imagining that there would be a
    > global error handler I could hook / override?


    Overriding report_callback_exception() seems to work:


    > from Tkinter import *


    import traceback
    import tkMessageBox

    >
    > class AppDemo(Frame):
    > """A sample tk app"""
    >
    > def __init__(self, parent=None):
    > Frame.__init__(self, parent)
    > self.pack(expand=YES, fill=BOTH)
    > self.make_tool_bar()
    > self.master.title("A Title")
    > self.bang = 0


    def show_error(*args):
    a = traceback.format_exception(*args)
    tkMessageBox.showerror(a[-1], "\n".join(a[:-1]))
    self._root().report_callback_exception = show_error

    > self.process()
    >
    > def process(self):
    > self.after(1000, self.process)
    > if self.bang:
    > self.bang = 0
    > raise ValueError("Callback Exception")
    >
    > def make_tool_bar(self):
    > self.toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
    > self.toolbar.pack(side=BOTTOM, fill=X)
    > Button(self.toolbar, text='Exception',
    > command=self.make_exception).pack(side=TOP, fill=X)
    > Button(self.toolbar, text='Callback Exception',
    > command=self.make_callback_exception).pack(side=TOP, fill=X)
    > Button(self.toolbar, text='Quit',
    > command=self.quit).pack(side=TOP, fill=X)
    >
    > def make_exception(self):
    > raise ValueError("Exception")
    >
    > def make_callback_exception(self):
    > self.bang = 1
    >
    > if __name__ == "__main__":
    > AppDemo().mainloop()


    Peter
    Peter Otten, Jul 26, 2006
    #2
    1. Advertising

  3. Peter Otten <> wrote:
    > Nick Craig-Wood wrote:
    > > How do you catch general exceptions in a Tkinter program.

    > Overriding report_callback_exception() seems to work:


    Thank you. That is exactly what I needed to know!

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
    Nick Craig-Wood, Jul 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    29,983
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,006
    Alex Martelli
    Nov 12, 2003
  3. Replies:
    3
    Views:
    596
    Sherm Pendley
    Apr 16, 2007
  4. Lie
    Replies:
    3
    Views:
    596
  5. Hidekazu IWAKI
    Replies:
    1
    Views:
    364
    Peter Otten
    Dec 14, 2009
Loading...

Share This Page