【发布时间】:2013-11-20 17:12:05
【问题描述】:
退出 GUI 时出现错误:
错误:“python.exe 已停止工作”
当我使用顶部菜单和工具栏退出选项退出时会发生这种情况。当我关闭右上角的“X”上的程序时也会发生这种情况。
但是,当我评论该行时:
self.mainToolBar.addAction(exitAction)
右上角的“X”不会出现此错误。
对于工具栏和顶部菜单上的退出选项,我使用的是:
exitAction.triggered.connect(qApp.quit)
关注代码:
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.topmenu()
self.toolbar()
self.resize(800, 600)
self.setWindowTitle('Example')
self.setWindowIcon(QtGui.QIcon('test.gif'))
self.show()
def topmenu(self):
#Buttons
exitAction = QAction(QtGui.QIcon('plus.gif'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(qApp.quit)
#Create MenuBar
menubar = self.menuBar()
#Add options
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
def toolbar(self):
exitAction = QAction(QtGui.QIcon('plus.gif'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setToolTip("Exit")
exitAction.triggered.connect(qApp.quit)
self.mainToolBar = QToolBar(self)
self.mainToolBar.setObjectName("mainToolBar")
self.addToolBar(Qt.LeftToolBarArea, self.mainToolBar)
# Line is giving the stop problem
self.mainToolBar.addAction(exitAction)
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我该如何解决这个问题?
【问题讨论】:
-
你不应该在某个地方定义
qApp吗?