473,805 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter and exceptions

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(expan d=YES, fill=BOTH)
self.make_tool_ bar()
self.master.tit le("A Title")
self.bang = 0
self.process()

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

def make_tool_bar(s elf):
self.toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
self.toolbar.pa ck(side=BOTTOM, fill=X)
Button(self.too lbar, text='Exception ', command=self.ma ke_exception).p ack(side=TOP, fill=X)
Button(self.too lbar, text='Callback Exception', command=self.ma ke_callback_exc eption).pack(si de=TOP, fill=X)
Button(self.too lbar, text='Quit', command=self.qu it).pack(side=T OP, fill=X)

def make_exception( self):
raise ValueError("Exc eption")

def make_callback_e xception(self):
self.bang = 1

if __name__ == "__main__":
AppDemo().mainl oop()

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 26 '06 #1
2 6337
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(expan d=YES, fill=BOTH)
self.make_tool_ bar()
self.master.tit le("A Title")
self.bang = 0
def show_error(*arg s):
a = traceback.forma t_exception(*ar gs)
tkMessageBox.sh owerror(a[-1], "\n".join(a[:-1]))
self._root().re port_callback_e xception = show_error
self.process()

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

def make_tool_bar(s elf):
self.toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
self.toolbar.pa ck(side=BOTTOM, fill=X)
Button(self.too lbar, text='Exception ',
command=self.ma ke_exception).p ack(side=TOP, fill=X)
Button(self.too lbar, text='Callback Exception',
command=self.ma ke_callback_exc eption).pack(si de=TOP, fill=X)
Button(self.too lbar, text='Quit',
command=self.qu it).pack(side=T OP, fill=X)

def make_exception( self):
raise ValueError("Exc eption")

def make_callback_e xception(self):
self.bang = 1

if __name__ == "__main__":
AppDemo().mainl oop()
Peter
Jul 26 '06 #2
Peter Otten <__*******@web. dewrote:
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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3356
by: Tom Locke | last post by:
Hi All, I'm having trouble with the python shell within emacs. It's hanging when I use tkinter. Setup is: Windows XP emacs 21.3 py-mode 4.6 Recipe:
2
4403
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate toplevel window) for displaying images and another thread for recording the frame rates (using a progress bar for visulization). The whole application worked very well once it received image data from the socket. The problem is when I tried to close that display window (click on the standard...
2
1490
by: Philippe C. Martin | last post by:
Hi, I have the following problem: I wrote a tkinter shell which on a key return event first evals the input buffer (in a try: except:) and then, in case of except, execs the input buffer. I have the problem if the exec fails:
6
18002
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance still exists and has a tkinter name, so testing for None is not feasible: >>> import Tkinter >>> fin = None >>> fin1 = Tkinter.Toplevel()
0
1228
by: msoulier | last post by:
In wxPython I install a top-level exception handler to intercept exceptions and display them in the GUI. With Tkinter, I'm trying to do the same. in __init__ sys.excepthook = self.ExceptionHandler def ExceptionHandler(self, type, value, tb):
1
3604
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings Panel. I just want it to bring up one. Is there an easy way to do that?
0
1549
by: wolfonenet | last post by:
Hi All, My setup is: WinXP Python 2.5.1 TKinter version: $Revision: 50704 $ Tcl: 8.4 Debugger: WinPdb
2
2556
by: Russell Blau | last post by:
I have some Tkinter programs that I run on two different machines. On Machine W, which runs Python 2.5.1 on Windows XP, these programs run fine. On Machine H, which runs Python 2.5.1 on Windows XP, however, the same programs crash regularly. The crashes are not Python exceptions, but rather are reported by Windows as errors in pythonw.exe. (Of course, the error messages themselves contain absolutely no useful information.) This happens...
3
4214
by: seanacais | last post by:
I'm trying to build an unknown number of repeating gui elements dynamically so I need to store the variables in a list of dictionaries. I understand that Scale "variable" name needs to be a StringVar but I cannot figure out how to initialize the dictionary. I've tried the following code ps = PowerSupply() # Instantiate a Power Supply VM Object numOPs = ps.getOnum() # Find the number of outputs OPValues = # Global list to...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.