PyQt5应用无法在Mac上正常终止

时间:2019-12-10 18:23:48

标签: python macos pyqt5

我有一个pyqt5 gui,我试图在Mac上运行。我用anaconda python写的。我发现,如果不包含sys.exit(app.exec_()),则运行时窗口永远不会打开。文档中会打开一个空白图标,但单击该按钮不会执行任何操作,也不会打开任何窗口。我必须强行退出才能使该图标消失。

使用sys.exit(app.exec_())时,窗口确实打开,并且应用程序完美运行。但是当我关闭窗口时,我得到了错误

An exception has occurred, use %tb to see the full traceback.

SystemExit: 0

/Users/aas03/opt/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3334: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

这会导致应用程序图标在扩展坞中保持打开状态,我必须强制退出以将其删除,从而导致内核重新启动。

但是,当我将sys.exit(app.exec_())替换为app.quit()时,它将导致python立即崩溃。

class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setWindowTitle("Blood/Multi-Blood Analyzer")
        self.setMinimumSize(550,150)


        layout=QGridLayout()
        groupbox=QGroupBox("Select File")
        vbox=QVBoxLayout()
        self.browseButton=QPushButton("Browse")
        self.browseButton.clicked.connect(self.browse)
        self.current_filename=""
        vbox.addWidget(self.browseButton)
        groupbox.setLayout(vbox)
        groupbox.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed)
        layout.addWidget(groupbox, 0, 0)
        self.fileLabel=QLabel("")
        layout.addWidget(self.fileLabel, 0,1)

        hlayout=QHBoxLayout()
        self.experimentLabel=QLabel("Select Experiment:")
        hlayout.addWidget(self.experimentLabel)
        self.var=""
        self.comboBox=QComboBox()
        self.comboBox.addItem("Blood")
        self.comboBox.addItem("Multi Blood")
        self.comboBox.activated.connect(self.selected)
        hlayout.addWidget(self.comboBox)
        layout.addLayout(hlayout,1,0)

        layout.setRowMinimumHeight(2,35)
        self.runButton=QPushButton("Run")
        self.runButton.setStyleSheet("background-color: green")
        self.runButton.clicked.connect(lambda: analysis(self,self.current_filename,self.var))
        layout.addWidget(self.runButton,3,0,1,3)



        widget=QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def browse(self):
        file=QFileDialog().getOpenFileName(self, "Open File","","CSV (*.csv);; Excel (*.xlsx)")
        self.current_filename=str(file[0])
        self.fileLabel.setText(self.current_filename)
    def selected(self):
        self.var=self.comboBox.currentText()

if __name__ == "__main__":
#    if not QApplication.instance():
    app = QApplication(sys.argv)
#    else:
#        app = QApplication.instance()
    QApplication.setStyle("Fusion")
    mainWin = Window()
    mainWin.show()
    sys.exit(app.exec_())

0 个答案:

没有答案