2014-05-09 78 views
0

我用wxGlade开发了一个GUI,它仍然在工作。但是为了启动GUI - 我编写了一些脚本并提供了一些选择。 所以一切正常,但当我用“x”按下红色按钮关闭窗口时 - 应用程序不会停止。退出按钮不工作 - wxpython

我做了一个方法,由一个单独的退出按钮调用,它在我的脚本中调用了一个退出函数。但通常用户正在使用关闭按钮(带X的红色按钮),所以我的方法没有被用于关闭窗口,并且窗口最终没有关闭。

这是退出功能。

def stopExport(self, event):      # wxGlade: MyFrame.<event_handler> 
    self.Close() # close the Frame 
    from ExportManager import Exportmanager  # import the exit function 
    Exportmanager().exit()      # call it 

我怎么可以使用此功能与红色按钮“x”?

+0

在程序中使用EVT_CLOSE事件吗?也许你应该在Close()之后调用Destroy()?不知道你的程序结构和事件处理很难回答。 –

回答

1

按我你的问题的理解,你的应用是不是当你点击关闭按钮,当你点击关闭按钮关闭(红色按钮与X在右上角)。

默认情况下你的应用程序应关闭。就你而言,在我看来,你已经将EVT_CLOSE绑定到某个方法,该方法没有代码来关闭应用程序窗口。 例如。考虑下面的代码片段,我有意将EVT_CLOSE事件绑定到名为closeWindow()的方法。这种方法没有做什么,这就是为什么我在那里有关键字。现在,如果您执行下面的代码片段,则可以看到应用程序窗口不会关闭。

代码

import wx 
class GUI(wx.Frame): 
    def __init__(self, parent, id, title): 
     screenWidth = 500 
     screenHeight = 400 
     screenSize = (screenWidth,screenHeight) 
     wx.Frame.__init__(self, None, id, title, size=screenSize) 
     self.Bind(wx.EVT_CLOSE, self.closeWindow) #Bind the EVT_CLOSE event to closeWindow() 

    def closeWindow(self, event): 
     pass #This won't let the app to close 

if __name__=='__main__': 
    app = wx.App(False) 
    frame = GUI(parent=None, id=-1, title="Problem Demo-PSS") 
    frame.Show() 
    app.MainLoop() 

因此,为了关闭应用程序窗口中,您需要更改closeWindow()。例如:当您点击关闭按钮时,以下代码段将使用Destroy()关闭应用程序窗口。

import wx 

class GUI(wx.Frame): 
    def __init__(self, parent, id, title): 
     screenWidth = 500 
     screenHeight = 400 
     screenSize = (screenWidth,screenHeight) 
     wx.Frame.__init__(self, None, id, title, size=screenSize) 
     self.Bind(wx.EVT_CLOSE, self.closeWindow) #Bind the EVT_CLOSE event to closeWindow() 

    def closeWindow(self, event): 
     self.Destroy() #This will close the app window. 


if __name__=='__main__': 
    app = wx.App(False) 
    frame = GUI(parent=None, id=-1, title="Problem Demo-PSS") 
    frame.Show() 
    app.MainLoop() 

我希望它是有用的。

+0

更好的答案可能是'event.Skip()'而不是'self.Destroy()'将控制权返回给默认事件处理程序。 – nepix32