终止QWidget不起作用

时间:2017-09-04 03:50:44

标签: python pyqt pyqt5

我使用self.close关闭正在运行的窗口小部件。整个代码如下:

class timeTracker(QWidget):
        '''time tracker main application v_0.8'''
    def __init__(self, parent=None):
        super(timeTracker, self).__init__(parent)
        self.grid = QGridLayout()
        self.setLayout(self.grid)

        while not self.connectivity():
            print(self.connectivity())
            ret = QMessageBox.warning(self, "Not connected to network. Please check the connection", "", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
            if ret == QMessageBox.No:
                self.close()
                qApp.quit()
                break
            qApp.quit()
        # self.time_widget = timeline.timeWidget()
        # self.grid.addWidget(self.time_widget, 0, 0)


    def connectivity(self):
        try:
            urllib.request.urlopen("http://www.google.com", timeout=1)
            return True
        except urllib.error.URLError as error:
            return False

if __name__ == "__main__":
    application = QApplication(sys.argv)
    main_widget = timeTracker()
    main_widget.show()
    main_widget.move(10, 10)
    sys.exit(application.exec_())

我对此代码的期望是,只要连接函数返回True,如果用户单击是,将反复要求用户执行某个操作。如果他们点击否,我想终止整个小部件。但是当我点击“否”时,它会在self.close之后通过这些行。我该怎么做才能真正终止程序?

1 个答案:

答案 0 :(得分:0)

要强行关闭应用程序,我们可以使用sys.exit()

if ret == QMessageBox.No:
    sys.exit(0)