我刚刚在pyqt中实现了登录,但是在调用url和获取响应之间,qt窗口显示未响应状态 谁能建议我如何避免不响应状态
代码:
class Login(QDialog):
def __init__(self, height, width, parent=None):
super(Login, self).__init__(parent)
self.resize(300, 300)
size = self.geometry()
self.move((width-size.width())/2, (height-size.height())/2)
username = QLabel("Username", self)
username.move(100, 35)
self.textName = QLineEdit(self)
password = QLabel("Password", self)
password.move(100, 110)
self.textPass = QLineEdit(self)
self.textPass.setEchoMode(QLineEdit.Password)
self.buttonLogin = QPushButton('Login', self)
self.buttonLogin.clicked.connect(self.handleLogin)
layout = QVBoxLayout(self)
layout.addWidget(self.textName)
layout.addWidget(self.textPass)
layout.addWidget(self.buttonLogin)
def handleLogin(self):
# TODO: Connect to flask application to check login credentials
payload = {"username":self.textName.text(),"password":self.textPass.text()}
print("payload:", payload)
r = requests.post("http://127.0.0.1:5000/login",data=payload)
print(r.content.decode("utf-8"))
if r.status_code == 200:
self.accept()
else:
QMessageBox.warning(self, 'Error', 'Bad user or password')
答案 0 :(得分:1)
1。使用线程:消耗大量时间的任务必须在另一个线程中执行
import requests
from PyQt5 import QtCore, QtWidgets
class LoginWorker(QtCore.QObject):
logged = QtCore.pyqtSignal(bool)
def setCredentials(self, username, password):
self.payload = {"username": username, "password": password}
@QtCore.pyqtSlot()
def login(self):
print("payload:", self.payload)
= requests.post("http://127.0.0.1:5000/login",data=self.payload)
print(r.content.decode("utf-8"))
status = r.status_code == 200
self.logged.emit(status)
class Login(QtWidgets.QDialog):
def __init__(self, height, width, parent=None):
super(Login, self).__init__(parent)
self.resize(300, 300)
size = self.geometry()
self.move((width-size.width())/2, (height-size.height())/2)
username = QtWidgets.QLabel("Username")
self.textName = QtWidgets.QLineEdit()
password = QtWidgets.QLabel("Password")
self.textPass = QtWidgets.QLineEdit()
self.textPass.setEchoMode(QtWidgets.QLineEdit.Password)
self.buttonLogin = QtWidgets.QPushButton('Login')
self.buttonLogin.clicked.connect(self.handleLogin)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(username, alignment=QtCore.Qt.AlignCenter)
layout.addWidget(self.textName)
layout.addWidget(password, alignment=QtCore.Qt.AlignCenter)
layout.addWidget(self.textPass)
layout.addWidget(self.buttonLogin)
layout.addStretch()
thread = QtCore.QThread(self)
thread.start()
self.login_worker = LoginWorker()
self.login_worker.moveToThread(thread)
self.login_worker.logged.connect(self.onLogged)
def handleLogin(self):
# TODO: Connect to flask application to check login credentials
self.login_worker.setCredentials(self.textName.text(), self.textPass.text())
QtCore.QTimer.singleShot(0, self.login_worker.login)
def onLogged(self, status):
if status:
self.accept()
else:
QtWidgets.QMessageBox.warning(self, 'Error', 'Bad user or password')
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Login(100, 100)
w.show()
sys.exit(app.exec_())
2。使用QNAM:
from PyQt5 import QtCore, QtWidgets, QtNetwork
class Login(QtWidgets.QDialog):
def __init__(self, height, width, parent=None):
super(Login, self).__init__(parent)
self.resize(300, 300)
size = self.geometry()
self.move((width-size.width())/2, (height-size.height())/2)
username = QtWidgets.QLabel("Username")
self.textName = QtWidgets.QLineEdit()
password = QtWidgets.QLabel("Password")
self.textPass = QtWidgets.QLineEdit()
self.textPass.setEchoMode(QtWidgets.QLineEdit.Password)
self.buttonLogin = QtWidgets.QPushButton('Login')
self.buttonLogin.clicked.connect(self.handleLogin)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(username, alignment=QtCore.Qt.AlignCenter)
layout.addWidget(self.textName)
layout.addWidget(password, alignment=QtCore.Qt.AlignCenter)
layout.addWidget(self.textPass)
layout.addWidget(self.buttonLogin)
layout.addStretch()
def handleLogin(self):
# TODO: Connect to flask application to check login credentials
postData = QtCore.QUrlQuery()
postData.addQueryItem("username", self.textName.text())
postData.addQueryItem("password", self.textPass.text())
qnam = QtNetwork.QNetworkAccessManager()
url = "http://127.0.0.1:5000/home"
reply = qnam.post(QtNetwork.QNetworkRequest(QtCore.QUrl(url)), postData.toString(QtCore.QUrl.FullyEncoded).encode())
loop = QtCore.QEventLoop()
reply.finished.connect(loop.quit)
loop.exec_()
code = reply.attribute(QtNetwork.QNetworkRequest.HttpStatusCodeAttribute)
if code == 200:
self.accept()
else:
QtWidgets.QMessageBox.warning(self, 'Error', 'Bad user or password')
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Login(100, 100)
w.show()
sys.exit(app.exec_())