我有一个具有登录屏幕的脚本,如果按下取消按钮,我想完全退出应用程序.我试过3种方法:
> sys.exit()
> QApplication.quit()
> QCoreApplication.instance().quit()
只有1号才有效.另外两个使对话框变白,然后闪烁然后挂起,我甚至无法切换到其他应用程序.我的代码如下:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5.QtWidgets import *
import csv
import sys
from datetime import datetime, timedelta, time
import os
from ci_co_table import *
from login import *
class Ci_Co(QMainWindow):
"""Check in and check out module"""
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Login(QDialog):
"""User login """
def __init__(self):
QDialog.__init__(self)
self.ui = Ui_login_form()
self.ui.setupUi(self)
self.ui.buttonBox.accepted.connect(lambda: self.handle_login(servers=servers))
servers = {}
with open('servers.csv', newline='') as csvfile:
server_reader = csv.reader(csvfile)
for row in server_reader:
self.ui.cbo_db_name.addItem(row[1])
servers[row[1]] = (row[0],row[2])
def handle_login(self, servers=''):
global user
global pword
global database
global server
global bg_colour
user = self.ui.username.text()
pword = self.ui.password.text()
database = self.ui.cbo_db_name.currentText()
server = servers[database][0]
bg_colour = servers[database][1]
if __name__=="__main__":
app=QApplication(sys.argv)
global hotdate
global hotdate_string
global folio_num
global user
global pword
global dbase
global server
pword = ""
global database
global bg_colour
#Login
while True:
if Login().exec_() == QDialog.Accepted:
db = QSqlDatabase.addDatabase("QPSQL");
db.setHostName(server)
db.setDatabaseName(database);
db.setUserName(user);
db.setPassword(pword)
if (db.open()==False):
QMessageBox.critical(None, "Database Error", db.lastError().text())
else:
break
else:
#QApplication.quit()
QCoreApplication.instance().quit()
#sys.exit()
myapp = Ci_Co()
myapp.show()
sys.exit(app.exec_())
解决方法:
调用QCoreApplication.quit()与调用QCoreApplication.exit(0)相同.引用qt docs:
After this function has been called, the application leaves the main
event loop and returns from the call to exec(). The exec() function
returns returnCode. If the event loop is not running, this function
does nothing. [emphasis added]
所以quit()或exit()与sys.exit()完全不同.后者将终止程序,但前者只会终止事件循环(如果它正在运行).
当用户取消登录对话框时,您的示例应该只调用sys.exit()来终止程序.否则,您的程序将陷入阻塞的while循环中.
标签:pyqt5,python,pyqt
来源: https://codeday.me/bug/20191008/1869770.html