我使用wxGlade开发了一个GUI,但它仍在运行。但是要启动GUI - 我编写了一些带有一些选择的脚本。 所以一切正常,但当我按下带有“x”的红色按钮关闭窗口时 - 应用程序不会停止。
我创建了一个方法,由一个单独的退出按钮调用,它在我的脚本中调用一个exit函数。但通常用户使用关闭按钮(带有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 :(得分:1)
根据我对您的问题的理解,当您点击关闭按钮(右上角带有X的红色按钮)时,您的应用程序没有关闭。
默认情况下,当您单击关闭按钮时,应用程序应该关闭。在您的情况下,在我看来,您已将EVT_CLOSE
绑定到某个方法,该方法中没有代码可以关闭应用程序窗口。
例如。考虑下面的代码片段,我有意将EVT_CLOSE
事件绑定到名为closeWindow()
的方法。这个方法没有做任何原因,因为那里有pass
关键字。现在,如果您执行下面的代码段,则可以看到应用程序窗口不会关闭。
<强>代码强>:
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()
我希望它很有用。