问题
After typing 1, both as login and password, a new window should appear, but it gives an error.
It used to work fine before I made some changes to code, and I don't know what exactly causes it.
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
app = QtGui.QApplication(sys.argv)
login = QtGui.QDialog()
login.setWindowTitle('login')
login.resize(100, 100)
login_form = QtGui.QFormLayout()
row1 = QtGui.QHBoxLayout()
user_input = QtGui.QLineEdit()
row1.addWidget(user_input)
login_form.addRow('user', row1)
row2 = QtGui.QHBoxLayout()
pwd_input = QtGui.QLineEdit()
row2.addWidget(pwd_input)
login_form.addRow('pwd', row2)
row3 = QtGui.QHBoxLayout()
login_btn = QtGui.QPushButton('LOGIN')
exit_btn = QtGui.QPushButton('EXIT')
row3.addWidget(login_btn)
row3.addWidget(exit_btn)
login_form.addRow(row3)
login.setLayout(login_form)
def handleLogin():
if (user_input.text() == '1' and
pwd_input.text() == '1'):
QtGui.QDialog.accept()
else:
QtGui.QMessageBox.warning(login, 'Error', 'Bad user or password',
buttons = QtGui.QMessageBox.Close,
defaultButton = QtGui.QMessageBox.Close)
QtCore.QObject.connect(login_btn, QtCore.SIGNAL('clicked()'), handleLogin)
if login.exec_() == QtGui.QDialog.Accepted:
window = QtGui.QWidget()
window.show()
sys.exit(app.exec_())
回答1:
The error happens because you are attepting to call a method via the class, rather than the instance. Try this instead:
def handleLogin():
if (user_input.text() == '1' and pwd_input.text() == '1'):
login.accept()
来源:https://stackoverflow.com/questions/40309352/how-to-fix-error-in-my-pyqt-programm-first-argument-of-unbound-method-must-have