简体   繁体   English

无法在Mac上关闭wxPython窗口

[英]Cannot close wxPython window on Mac

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. 当我尝试将vtk集成到wxPython gui中时,我一直在努力解决我现在意识到的几种不同的python崩溃和/或应用程序挂起的问题。 I have a separate open question that deals with a stand-alone vtk example. 我有一个单独的开放式问题 ,涉及一个独立的vtk示例。 I have also found a class of problems surrounding proper deletion of vtk objects during the window closing process. 我还发现了一类问题,涉及在关闭窗口过程中正确删除vtk对象。 However, in chasing those issues I have discovered that I can't even cleanly close a simple wx.Frame! 但是,在追踪这些问题时,我发现我什至无法干净地关闭一个简单的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). 以下简单示例(作为对关闭wxPython的其他人的不相关问题的答案的一部分)无法在我的Mac 上关闭 (在Mac OSX 10.9.4上运行Enthought Canopy 1.4.1.1975),但在我的Windows VM上运行并干净地关闭了(运行相同的Canopy版本)。

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. 具体来说,单击红色的“关闭”点或“关闭” wx.Button可以将控制权返回给集成在Canopy中的python提示符,但是窗口不会消失。 With the "Close" button I see the print statement appear as control passes through the onClose function. 通过“关闭”按钮,我看到打印语句出现在控件通过onClose函数传递时。 In both cases, restarting the python kernel (run->"Restart Kernel..." in Canopy menu bar) makes the window go away. 在这两种情况下,重新启动python内核(Canopy菜单栏中的run->“ Restart Kernel ...”)都会使窗口消失。

Pylab is already disabled due to troubleshooting the previous issue mentioned above. 由于对上述问题进行了故障排除,因此Pylab已被禁用。

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: 编辑:我尝试从命令行(而不是通过Canopy的IDE)运行上述脚本,并观察到以下内容:

  • [Non Interactive] Typing "python simpleClose.py" at the command line the window launches and then closes normally to all appearances [非交互式]在命令行中键入“ python simpleClose.py”,窗口启动,然后正常关闭所有外观

  • [Interactive] Typing "python" at the command line followed by "execfile('simpleClose.py')" at the interactive python prompt the window launches but does not close. [交互式]在命令行中键入“ python”,然后在交互式python中键入“ execfile('simpleClose.py')”,提示窗口启动但未关闭。 After pressing the close button, control returns to the interactive python prompt (I can execute further commands) but the window stays open. 按下关闭按钮后,控件返回到交互式python提示符(我可以执行其他命令),但窗口保持打开状态。 After typing "exit()" to close the interactive python prompt the window goes away as well. 键入“ exit()”以关闭交互式python提示后,窗口也会消失。 If instead, I try to relaunch the test application, I receive a segfault as shown below: 如果相反,我尝试重新启动测试应用程序,则会收到以下所示的段错误:

[mac prompt]$ python [mac提示符] $ python

Enthought Canopy Python 2.7.6 | 热情的Canopy Python 2.7.6 | 64-bit | 64位| (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. (默认值,2014年6月4日,16:42:26)在darwin上使用[GCC 4.2.1(Apple Inc. build 5666)(第3点)]输入更多的“帮助”,“版权”,“信用”或“许可证”信息。

[python prompt] execfile('simpleClose.py') [python提示符] execfile('simpleClose.py')

onClose 的OnClose

[python prompt] execfile('simpleClose.py') [python提示符] execfile('simpleClose.py')

Traceback (most recent call last): 追溯(最近一次通话):
File "[stdin]", line 1, in [module] [模块]中第1行的文件“ [stdin]”
File "simpleClose.py", line 25, in [module] frame = MyFrame() File "simpleClose.py", line 10, in init wx.Frame. 文件[simpleClose.py],第25行,在[模块]框架中= MyFrame()文件“ simpleClose.py”,第10行,在初始化 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! init (自身,无,标题=“ Close Me”)>文件“ /Users/tanner/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/wx/_windows.py”,行576,位于初始化 Windows .Frame_swiginit(自身, Windows .new_Frame(* args,** kwargs))wx._core.PyNoAppError:必须首先创建wx.App对象!

[python prompt] exit() [python提示符] exit()

Segmentation fault: 11 细分错误:11

[mac prompt]$ [mac提示符] $

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. 与乔纳森·马奇(Jonathan March)针对我在帖子顶部引用的另一个未解决问题的提议解决方案直接冲突,当我在Canopy首选项下重新启用PyLab时,此问题消失了。 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...). 我注意到我所有的matplotlib图都不再可交互(尝试旋转的沙滩球光标再也不会消失,并且我无法缩放,旋转等)之后,我尝试了此操作。 Re-enabling PyLab fixed the plot issue as well. 重新启用PyLab也解决了绘图问题。 I am currently using the QT backend for PyLab and have not tried using any of the other back-ends available. 我目前正在使用PyLab的QT后端,还没有尝试使用任何其他可用的后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM