Couple of event-related questions

In a program I'm developing, I notice some differences in behavior
between when it is run in console mode with the Python interpreter
versus when it is run as a Windows program created by py2exe.

First, dialogs get canceled with the Escape button in the console
version, but usually not in the gui version. I will try to figure out
why different events are being raised or handled, but thought I would
ask if anyone happens to have a good guess as to why this would happen.

Secondly, the GUI version sometimes ends with a message box saying that
errors have been logged to a file. The file describes exceptions that
were raised. Although this is useful during debugging, I would rather
the program closed silently for users when there was no fatal crash.
How can I eliminate the message box?

Jamal

Jamal Mazrui wrote:

First, dialogs get canceled with the Escape button in the console
version, but usually not in the gui version.

I'm totally guessing here, but it may be something to do with what manifest file is being used -- are you including a manifest in your py2exe build?

Secondly, the GUI version sometimes ends with a message box saying that
errors have been logged to a file. The file describes exceptions that
were raised. Although this is useful during debugging, I would rather
the program closed silently for users when there was no fatal crash.
How can I eliminate the message box?

This is a py2exe issue. That system is set up in py2exe boot script, and I think it's there whether you like it or not. So if anything gets sent to stderr, that dialog will pop up (I wish it could be turned off, but I couldn't figure out how). You could hack the py2exe code, but I didn't want to get into that.

However, what you can do is add your own custom boot script, that will run after py2exe's. In there you can re-direct stderr, so that nothing will ever get written to it, and then the dialog will never pop up. I've also optionally re-directed the stdout and stderr to a log file.

Here's how you include your own boot script:

     OPTIONS = { ...
                 # This is run when the executable starts up, after
                 # py2exe's normal boot script(s)
                 'custom_boot_script' : 'py2exe_boot.py',
                 }

and:

     setup( name = NAME,
            #console =
            windows=
                 [{ 'script': APP[0],
                    'dest_base': NAME,
                    'icon_resources': [(1, "CameoWeb.ico")],
                    'other_resources': [(24, 1, manifest)],
                  }],
            data_files = DATA_FILES,
            #version = VERSION,
            description = DESCRIPTION,
            author = AUTHOR,
            author_email = AUTHOR_EMAIL,
            options = {'py2exe': OPTIONS},
            setup_requires = ['py2exe'],
            zipfile = 'library/library.pyz',
            )

And here's what I have in my boot script:

import os, sys

## redirect stdout and strerr:
if os.path.isfile('noaa.ver'):
     # direct all output to logfile
     sys.stdout = sys.stderr
else:
     ## Send both strerr and stout to "balckHole" so logfile doesn't get written at all.
     sys.stderr = sys.stdout

I'm pretty sure that stdout gets set to nothing by the py2exe boot script, for a "windows" application.

If you find a cleaner way to do this, please let us know! (and post it on the py2exe Wiki)

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks, Chris. I'll try the custom script when I get a chance.

Regarding Escape not canceling dialogs in the GUI version, I've
investigated further but without success. I'm not using a manifest
file. In the console version, Escape triggers a command event, but not
in the GUI version. In general, no event occurs, though curiously, I've
found that when focus is in a multi-line TextCtrl, the same command
event usually (but not always) occurs. When focus is on a ListBox or
Button, no event occurs.

I have tried two work-arounds within my custom Dialog class. The first
tries to make Escape an accelerator key that sends the Cancel ID:

at = wx.AcceleratorTable([(0, wx.WXK_ESCAPE, wx.ID_CANCEL)])
self.SetAcceleratorTable(at)

The second explicitly sets the Cancel ID as the Escape ID of the dialog:

    self.SetEscapeId(wx.ID_CANCEL)

Unfortunately neither attempt made a difference!

Jamal

P.S. Please forgive the top-posting folks -- I happen to be blind and
this is *much* easier for me to do when writing email with my screen
reader technology.

···

-----Original Message-----
From: wxpython-users-bounces@lists.wxwidgets.org
[mailto:wxpython-users-bounces@lists.wxwidgets.org] On Behalf Of
Christopher Barker
Sent: Friday, April 24, 2009 12:43 PM
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Couple of event-related questions

Jamal Mazrui wrote:

First, dialogs get canceled with the Escape button in the console
version, but usually not in the gui version.

I'm totally guessing here, but it may be something to do with what
manifest file is being used -- are you including a manifest in your
py2exe build?

Secondly, the GUI version sometimes ends with a message box saying
that errors have been logged to a file. The file describes exceptions

that were raised. Although this is useful during debugging, I would
rather the program closed silently for users when there was no fatal

crash.

How can I eliminate the message box?

This is a py2exe issue. That system is set up in py2exe boot script, and
I think it's there whether you like it or not. So if anything gets sent
to stderr, that dialog will pop up (I wish it could be turned off, but I
couldn't figure out how). You could hack the py2exe code, but I didn't
want to get into that.

However, what you can do is add your own custom boot script, that will
run after py2exe's. In there you can re-direct stderr, so that nothing
will ever get written to it, and then the dialog will never pop up. I've
also optionally re-directed the stdout and stderr to a log file.

Here's how you include your own boot script:

     OPTIONS = { ...
                 # This is run when the executable starts up, after
                 # py2exe's normal boot script(s)
                 'custom_boot_script' : 'py2exe_boot.py',
                 }

and:

     setup( name = NAME,
            #console =
            windows=
                 [{ 'script': APP[0],
                    'dest_base': NAME,
                    'icon_resources': [(1, "CameoWeb.ico")],
                    'other_resources': [(24, 1, manifest)],
                  }],
            data_files = DATA_FILES,
            #version = VERSION,
            description = DESCRIPTION,
            author = AUTHOR,
            author_email = AUTHOR_EMAIL,
            options = {'py2exe': OPTIONS},
            setup_requires = ['py2exe'],
            zipfile = 'library/library.pyz',
            )

And here's what I have in my boot script:

import os, sys

## redirect stdout and strerr:
if os.path.isfile('noaa.ver'):
     # direct all output to logfile
     sys.stdout = sys.stderr
else:
     ## Send both strerr and stout to "balckHole" so logfile doesn't get
written at all.
     sys.stderr = sys.stdout

I'm pretty sure that stdout gets set to nothing by the py2exe boot
script, for a "windows" application.

If you find a cleaner way to do this, please let us know! (and post it
on the py2exe Wiki)

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users