简体   繁体   English

如何解决我的pyqt程序中的错误(未绑定方法的第一个参数必须具有“ QDialog”类型)?

[英]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. 输入1作为登录名和密码后,应该会出现一个新窗口,但是会出现错误。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:QPixmap.toImage():未绑定方法的第一个参数必须具有类型“ QPixmap” - TypeError: QPixmap.toImage(): first argument of unbound method must have type 'QPixmap' TypeError:QFileDialog.history(self):未绑定方法的第一个参数必须具有类型“ QFileDialog” - TypeError: QFileDialog.history(self): first argument of unbound method must have type 'QFileDialog' 如何修复TypeError:必须使用Dropper实例作为第一个参数来调用未绑定方法 - How to fix TypeError: unbound method must be called with Dropper instance as first argument Python错误“必须以实例作为第一个参数来调用未绑定方法” - Python error “unbound method must be called with instance as first argument” 必须使用instance作为第一个参数调用unbound方法 - unbound method must be called with instance as first argument 错误:必须使用“类名”实例作为第一个参数来调用未绑定方法“方法名”(取而代之的是classobj对象) - ERROR: unbound method “method name” must be called with “Class Name” instance as first argument (got classobj instance instead) 错误消息:“ TypeError:必须使用日志实例作为第一个参数来调用未绑定方法w()(而是使用got str实例)” - Error message: “TypeError: unbound method w() must be called with log instance as first argument (got str instance instead)” 错误:TypeError:未绑定方法__init __()必须以shape实例作为第一个参数调用(代替str实例) - # Error: TypeError: unbound method __init__() must be called with shape instance as first argument (got str instance instead) # 错误:未绑定的方法Dragon()必须以Enemy实例作为第一个参数来调用(取而代之的是Player实例) - Error: unbound method Dragon( ) must be called with Enemy instance as first argument (got Player instance instead) 必须使用instance作为第一个参数调用unbound方法 - python - unbound method must be called with instance as first argument - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM