python - Window close does not close program, even with a function calling sys.exit() bound to the window's close_event. What might cause this? -
the program below sets graph , displays blue box each time user clicks. user can make more boxes using right-click popup menu. when implemented popup menu (thus allowing multiple blue boxes) program stopped closing on window close. connected deleteall() method canvas force sys.exit() on close, program still stays open. doing wrong?
#**************************************************************************# # set graph. click creates blue box. more blue boxes can made # choosing "make new box" right/click menu. #**************************************************************************# import wx import matplotlib matplotlib.use('wx') import matplotlib.pyplot plt import matplotlib.patches patches matplotlib.figure import figure matplotlib.backends.backend_wxagg import figurecanvaswxagg figcanv pylab import * #**************************************************************************# class maincanvas(wx.frame): """ class set graph """ def __init__(self): wx.frame.__init__(self,none,-1, 'make blue boxes',size=(550,350)) #**********************************************************************# def setupgraph(self): """ set window , graph """ self.defineplot() self.canvas = figcanv(self, -1, self.figure) self.setsizer() #**********************************************************************# def defineplot(self): """ define attributes of graph """ self.setbackgroundcolour(wx.namedcolor("white")) self.figure = figure() self.axes = self.figure.add_subplot(111) #**********************************************************************# def setsizer(self): """ set size of graph """ self.sizer = wx.boxsizer(wx.vertical) self.sizer.add(self.canvas, 1, wx.left | wx.top | wx.grow) self.setsizer(self.sizer) self.fit() #**************************************************************************# class bluebox(maincanvas): """ class set bluebox """ def __init__(self): maincanvas.__init__(self) #**********************************************************************# def setupbluebox(self): """ blue box set """ self.bluebox = patches.rectangle((0, 0), 0.05, 0.05, fc = "blue", visible = false, zorder = 3) # set zorder new boxes in front of old if len(self.listdt)>1: oldz = self.listdt[len(self.listdt)-2].bluebox.get_zorder() + 3 self.listdt[len(self.listdt)-1].bluebox.set_zorder(oldz) self.axes.add_patch(self.bluebox) #**********************************************************************# def onpick(self, event): """ behavior when graph clicked """ # refresh bluebox position , coordinate text self.refreshbluebox(event) # set current bluebox visible self.listdt[len(self.listdt)-1].setvisible() #**********************************************************************# def refreshbluebox(self, event): """ blue box refresh """ # center bluebox (point box) on mouse click location self.bluebox.set_x(event.xdata - .5 * self.bluebox.get_width()) self.bluebox.set_y(event.ydata - .5 * self.bluebox.get_height()) #**********************************************************************# def setvisible(self): """ make bluebox visible , refresh canvas """ self.bluebox.set_visible(true) self.figure.canvas.draw() #**********************************************************************# def setgraphattributes(self, axes, figure, listdt): """ tell each bluebox graph attributes needs """ self.axes = axes self.figure = figure self.listdt = listdt #**************************************************************************# class mypopupmenu(bluebox): """ class handle right clicks """ def __init__(self): bluebox.__init__(self) #**********************************************************************# def setup(self): """ set graph , bluebox """ self.setupgraph() # bind right clicks open popup menu self.canvas.bind(wx.evt_right_down, self.createpopupmenu) # create lists blueboxs , binding ids self.listdt = [] self.listbid = [] self.popupcreatenew = wx.newid() self.menu = wx.menu() self.cn = self.menu.append(self.popupcreatenew, "make new box") self.bind(wx.evt_menu, self.createnew, self.cn) # make first bluebox self.newbox() # connect close events delete self.figure.canvas.mpl_connect('close_event', self.deleteall) #**********************************************************************# def deleteall(self, event): """ close out of program cleanly """ self.destroy() sys.exit(0) #**********************************************************************# def createpopupmenu(self, event): """ create parts of right-click popup menu """ self.popupmenu(self.menu) #**********************************************************************# def newbox(self): """ make new bluebox """ self.listdt.append(bluebox()) self.listdt[len(self.listdt)-1].setgraphattributes(self.axes, self.figure, self.listdt) self.listdt[len(self.listdt)-1].setupbluebox() self.listbid.append(self.figure.canvas.mpl_connect('button_press_event', self.listdt[len(self.listdt)-1].onpick)) #**********************************************************************# def createnew(self, event): """ create new bluebox """ event.skip() self.newbox() if len(self.listbid) > 1: self.figure.canvas.mpl_disconnect(self.listbid[len(self.listbid)-2]) #**************************************************************************# class app(wx.app): """ create , show """ def oninit(self): self.mpm = mypopupmenu() self.mpm.setup() self.frame = self.mpm.show() return true #**************************************************************************# # run program #**************************************************************************# app = app(0) app.mainloop()
use app.exit() or app.exitmainloop() exit main loop instead.
Comments
Post a Comment