Question

I have been struggling with what I now realize are several different python crashes and/or application hangs as I try to integrate vtk in a wxPython gui. I have a separate open question that deals with a stand-alone vtk example. I have also found a class of problems surrounding proper deletion of vtk objects during the window closing process. However, in chasing those issues I have discovered that I can't even cleanly close a simple wx.Frame!

The following simple example (provided as part of an answer to someone else's unrelated question on closing wxPython ) fails to close on my Mac (running Enthought Canopy 1.4.1.1975 on Mac OSX 10.9.4) but runs and closes cleanly on my windows VM (running same Canopy version).

Specifically, clicking either the red "close" dot or the "Close" wx.Button returns control to the python prompt integrated in Canopy, but the window does not go away. With the "Close" button I see the print statement appear as control passes through the onClose function. In both cases, restarting the python kernel (run->"Restart Kernel..." in Canopy menu bar) makes the window go away.

Pylab is already disabled due to troubleshooting the previous issue mentioned above.

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Close Me")
        panel = wx.Panel(self)

        closeBtn = wx.Button(panel, label="Close")
        closeBtn.Bind(wx.EVT_BUTTON, self.onClose)

    #----------------------------------------------------------------------
    def onClose(self, event):
        """"""
        print 'onClose'
        self.Close()


if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

Edit: I tried running the above script from the command line (instead of through Canopy's IDE) and observed the following:

  • [Non Interactive] Typing "python simpleClose.py" at the command line the window launches and then closes normally to all appearances

  • [Interactive] Typing "python" at the command line followed by "execfile('simpleClose.py')" at the interactive python prompt the window launches but does not close. After pressing the close button, control returns to the interactive python prompt (I can execute further commands) but the window stays open. After typing "exit()" to close the interactive python prompt the window goes away as well. If instead, I try to relaunch the test application, I receive a segfault as shown below:

[mac prompt]$ python

Enthought Canopy Python 2.7.6 | 64-bit | (default, Jun 4 2014, 16:42:26) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information.

[python prompt] execfile('simpleClose.py')

onClose

[python prompt] execfile('simpleClose.py')

Traceback (most recent call last):
File "[stdin]", line 1, in [module]
File "simpleClose.py", line 25, in [module] frame = MyFrame() File "simpleClose.py", line 10, in init wx.Frame.init(self, None, title="Close Me") > File "/Users/tanner/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/wx/_windows.py", line 576, in init windows.Frame_swiginit(self,windows.new_Frame(*args, **kwargs)) wx._core.PyNoAppError: The wx.App object must be created first!

[python prompt] exit()

Segmentation fault: 11

[mac prompt]$

Was it helpful?

Solution

This is a wholly unsatisfying answer because I do not yet understand what is fundamentally going on, but I'm putting it up because it did fix my problem (for now). I am still hoping that someone else will post a better answer that sheds light on the underlying issues.

In direct conflict with the proposed solution by Jonathan March to my separate open question referenced at the top of the post, this problem went away when I re-enabled PyLab under the Canopy preferences. I tried this after I noticed that all of my matplotlib plots were no longer interactable (The spinning beach ball cursor never went away and I could not zoom, rotate, etc...). Re-enabling PyLab fixed the plot issue as well. I am currently using the QT backend for PyLab and have not tried using any of the other back-ends available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top