<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://ascend4.org/index.php?action=history&amp;feed=atom&amp;title=Showing_an_error_dialog_with_pythonw.exe</id>
	<title>Showing an error dialog with pythonw.exe - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://ascend4.org/index.php?action=history&amp;feed=atom&amp;title=Showing_an_error_dialog_with_pythonw.exe"/>
	<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=Showing_an_error_dialog_with_pythonw.exe&amp;action=history"/>
	<updated>2026-06-16T17:09:28Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://ascend4.org/index.php?title=Showing_an_error_dialog_with_pythonw.exe&amp;diff=984&amp;oldid=prev</id>
		<title>Jpye at 02:57, 20 July 2010</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=Showing_an_error_dialog_with_pythonw.exe&amp;diff=984&amp;oldid=prev"/>
		<updated>2010-07-20T02:57:40Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 02:57, 20 July 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l48&quot;&gt;Line 48:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 48:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/source&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/source&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;EDIT&lt;/del&gt;: &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;This is how it looks like in PyMail, one of my scripts-that-grew-into-an-app ;&lt;/del&gt;)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Here&#039;s a little screenshot that shows the exception being thrown by attempts to make too much sys.stdout output from within pythonw (eg see [http&lt;/ins&gt;:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;//groups.google.com/group/comp.lang.python/browse_thread/thread/18d054248d4d4c01/98939813757c7c22?lnk=gst&amp;amp;q=pythonw#98939813757c7c22 here]&lt;/ins&gt;)&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Image:Pythonw-error-dialog.png|thumb|400px|none|Screenshot of a Python Stacktrace in a Messagebox]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Image:Pythonw-error-dialog.png|thumb|400px|none|Screenshot of a Python Stacktrace in a Messagebox]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Miscellany]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Miscellany]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key mediawiki:diff:1.41:old-982:rev-984:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Jpye</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=Showing_an_error_dialog_with_pythonw.exe&amp;diff=982&amp;oldid=prev</id>
		<title>Jpye: Created page with &#039;This content has been adapted from Google&#039;s cache of http://arunrocks.com, which at this time seems to be offline.  &#039;&#039;&#039;Making Python Scripts Show Windows-friendly Errors/Stacktra…&#039;</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=Showing_an_error_dialog_with_pythonw.exe&amp;diff=982&amp;oldid=prev"/>
		<updated>2010-07-20T02:55:13Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;#039;This content has been adapted from Google&amp;#039;s cache of http://arunrocks.com, which at this time seems to be offline.  &amp;#039;&amp;#039;&amp;#039;Making Python Scripts Show Windows-friendly Errors/Stacktra…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This content has been adapted from Google&amp;#039;s cache of http://arunrocks.com, which at this time seems to be offline.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Making Python Scripts Show Windows-friendly Errors/Stacktrace&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
:2007 June 20&lt;br /&gt;
:by Arun Bhai&lt;br /&gt;
&lt;br /&gt;
Most of us love to distribute our python programs to others once you have finished coding a neat little script. For Windows users we package it using Py2exe or cx_freeze. However, many of the end-users will not be happy with a black command window popping up, say, when an error is thrown.&lt;br /&gt;
&lt;br /&gt;
Of course the alternative is to write a full blown GUI application using WXPython or PyFLTK. Even the latter, though quite lightweight, adds several megabytes to the distribution, when all you need is a simple message-box indicating an error or showing some informational text. Clearly, its an overkill for your throwaway python scripts.&lt;br /&gt;
&lt;br /&gt;
This is the kind of problem I typically face and I have found a good solution. The answer is ctypes library which comes as a part of the standard distribution from Python 2.5 onwards. It simply calls the messagebox function from user32.dll (which is always present in a windows installation). With the main problem solved, what remained was to obtain the error text and stack trace.&lt;br /&gt;
&lt;br /&gt;
Let’s see how the code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
# Importing all the works for a native Win32 Message Box&lt;br /&gt;
from ctypes import c_int, WINFUNCTYPE, windll&lt;br /&gt;
from ctypes.wintypes import HWND, LPCSTR, UINT&lt;br /&gt;
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)&lt;br /&gt;
paramflags = (1, &amp;quot;hwnd&amp;quot;, 0), (1, &amp;quot;text&amp;quot;, &amp;quot;Hi&amp;quot;), (1, &amp;quot;caption&amp;quot;, None), (1, &amp;quot;flags&amp;quot;, 0)&lt;br /&gt;
MessageBox = prototype((&amp;quot;MessageBoxA&amp;quot;, windll.user32), paramflags)&lt;br /&gt;
&lt;br /&gt;
# For printing the stack&lt;br /&gt;
import sys&lt;br /&gt;
import traceback&lt;br /&gt;
from time import sleep&lt;br /&gt;
&lt;br /&gt;
def show_popup(text):&lt;br /&gt;
    print text&lt;br /&gt;
    MessageBox(text=text, caption=&amp;quot;Sample App Says...&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
def mainloop():&lt;br /&gt;
    raise &amp;quot;Uff!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    try:&lt;br /&gt;
        mainloop()&lt;br /&gt;
    except:&lt;br /&gt;
        type, value, sys.last_traceback = sys.exc_info()&lt;br /&gt;
        lines = traceback.format_exception(type, value,sys.last_traceback)&lt;br /&gt;
        show_popup(&amp;quot;Aiyooooo..... there has been an error!\n&amp;quot; +&lt;br /&gt;
            &amp;quot;Exception in user code:\n&amp;quot; +&lt;br /&gt;
            &amp;quot;&amp;quot;.join(lines) +&lt;br /&gt;
            &amp;quot;===== Please mail a screenshot to arunvr@gmail.com ====&amp;quot;&lt;br /&gt;
            )&lt;br /&gt;
    finally:&lt;br /&gt;
        sleep(1) # show the console output for a second so that users can read it&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
EDIT: This is how it looks like in PyMail, one of my scripts-that-grew-into-an-app ;)&lt;br /&gt;
&lt;br /&gt;
[[Image:Pythonw-error-dialog.png|thumb|400px|none|Screenshot of a Python Stacktrace in a Messagebox]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Miscellany]]&lt;/div&gt;</summary>
		<author><name>Jpye</name></author>
	</entry>
</feed>