Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

python - How to fix error in my pyqt programm (first argument of unbound method must have type 'QDialog') ?

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_())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...