如何解决:TypeError:参数1具有意外的类型'QWidget'

时间:2019-08-08 11:11:38

标签: python pyqt5 qt-creator

按钮单击def'log_in'结果为'TypeError' 但是,当我将按钮更改为def'login_failed'时,则已经可以使用

我是PyQT5的新手,我不明白为什么会收到此错误。将创建def谁将检查登录名和密码文本标签,但我没有从中获得价值。用QT Creator创建的代码。

我的代码:

从PyQt5导入QtCore,QtGui,QtWidgets 从等待时间导入睡眠

class Ui_MainWindow(object):

    def login_failed(self):
        self.error_login.setStyleSheet("color: #D92525;")

    def log_in(self):
        print('Your name: ' + self.line_login.text())

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(500, 600)
        MainWindow.setMinimumSize(QtCore.QSize(500, 600))
        MainWindow.setMaximumSize(QtCore.QSize(500, 600))
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet("background-color: #1D2226;")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.log_in = QtWidgets.QWidget(self.centralwidget)
        self.log_in.setGeometry(QtCore.QRect(100, 200, 300, 191))
        self.log_in.setMinimumSize(QtCore.QSize(300, 0))
        self.log_in.setMaximumSize(QtCore.QSize(300, 16777215))
        self.log_in.setAutoFillBackground(False)
        self.log_in.setObjectName("log_in")
        self.line_login = QtWidgets.QLineEdit(self.log_in)
        self.line_login.setGeometry(QtCore.QRect(50, 60, 201, 25))
        self.line_login.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.line_login.setAutoFillBackground(False)
        self.line_login.setStyleSheet("color: #F0F1F2; border: 1px solid black")
        self.line_login.setObjectName("lineEdit")
        self.line_pass = QtWidgets.QLineEdit(self.log_in)
        self.line_pass.setGeometry(QtCore.QRect(50, 140, 201, 25))
        self.line_pass.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.line_pass.setAutoFillBackground(False)
        self.line_pass.setStyleSheet("color: #F0F1F2; border: 1px solid black")
        self.line_pass.setEchoMode(QtWidgets.QLineEdit.Password)
        self.line_pass.setObjectName("lineEdit_2")
        self.label = QtWidgets.QLabel(self.log_in)
        self.label.setGeometry(QtCore.QRect(120, 20, 54, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setStyleSheet("color: #F0F1F2")
        self.label.setLocale(QtCore.QLocale(QtCore.QLocale.Polish, QtCore.QLocale.Poland))
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.log_in)
        self.label_2.setGeometry(QtCore.QRect(120, 100, 54, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color: #F0F1F2;")
        self.label_2.setLocale(QtCore.QLocale(QtCore.QLocale.Polish, QtCore.QLocale.Poland))
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(140, 50, 221, 91))
        self.label_3.setText("")
        self.label_3.setPixmap(QtGui.QPixmap("images/white-logo.png"))
        self.label_3.setScaledContents(True)
        self.label_3.setObjectName("label_3")
        self.captcha_checkbox = QtWidgets.QCheckBox(self.centralwidget)
        self.captcha_checkbox.setEnabled(True)
        self.captcha_checkbox.setGeometry(QtCore.QRect(360, 540, 111, 16))
        self.captcha_checkbox.setFocusPolicy(QtCore.Qt.NoFocus)
        self.captcha_checkbox.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.captcha_checkbox.setStyleSheet("color: #F0F1F2;")
        self.captcha_checkbox.setChecked(False)
        self.captcha_checkbox.setTristate(False)
        self.captcha_checkbox.setObjectName("captcha_checkbox")
        self.login_button = QtWidgets.QPushButton(self.centralwidget)
        self.login_button.setGeometry(QtCore.QRect(200, 450, 100, 32))
        self.login_button.setMinimumSize(QtCore.QSize(100, 32))
        self.login_button.setMaximumSize(QtCore.QSize(100, 32))




        #################### BUTTON EVENT ####################

        self.login_button.clicked.connect(self.log_in)

        #################### BUTTON EVENT ####################



        font = QtGui.QFont()
        font.setPointSize(11)
        self.login_button.setFont(font)
        self.login_button.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.login_button.setStyleSheet("background-color: #1D2226; color: #F0F1F2 ; border: 1px solid black")
        self.login_button.setLocale(QtCore.QLocale(QtCore.QLocale.Polish, QtCore.QLocale.Poland))
        self.login_button.setFlat(False)
        self.login_button.setObjectName("login_button")
        self.widget_captcha = QtWidgets.QWidget(self.centralwidget)
        self.widget_captcha.setGeometry(QtCore.QRect(20, 490, 111, 71))
        self.widget_captcha.setObjectName("widget_captcha")
        self.captcha_label = QtWidgets.QLabel(self.widget_captcha)
        self.captcha_label.setGeometry(QtCore.QRect(20, 10, 71, 21))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.captcha_label.setFont(font)
        self.captcha_label.setContextMenuPolicy(QtCore.Qt.PreventContextMenu)
        self.captcha_label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.captcha_label.setAutoFillBackground(False)
        self.captcha_label.setStyleSheet("color: #F0F1F2; text-align: center;")
        self.captcha_label.setAlignment(QtCore.Qt.AlignCenter)
        self.captcha_label.setObjectName("captcha_label")
        self.captcha_line = QtWidgets.QLineEdit(self.widget_captcha)
        self.captcha_line.setEnabled(True)
        self.captcha_line.setGeometry(QtCore.QRect(10, 40, 91, 21))
        self.captcha_line.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.captcha_line.setStyleSheet("color: #F0F1F2; border: 1px solid black")
        self.captcha_line.setObjectName("captcha_line")
        self.error_login = QtWidgets.QLabel(self.centralwidget)
        self.error_login.setGeometry(QtCore.QRect(160, 410, 181, 21))
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(False)
        font.setWeight(50)
        self.error_login.setFont(font)
        self.error_login.setStyleSheet("color: #1D2226;")
        self.error_login.setAlignment(QtCore.Qt.AlignCenter)
        self.error_login.setObjectName("error_login")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.captcha_checkbox.stateChanged['int'].connect(self.widget_captcha.hide)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Frekfencja ZSOT"))
        self.label.setText(_translate("MainWindow", "Login"))
        self.label_2.setText(_translate("MainWindow", "Hasło"))
        self.captcha_checkbox.setText(_translate("MainWindow", "Auto Captcha"))
        self.login_button.setText(_translate("MainWindow", "Zaloguj"))
        self.captcha_label.setText(_translate("MainWindow", "Captcha"))
        self.error_login.setText(_translate("MainWindow", "Nie udało się zalogować"))


    import sys <br>
    app = QtWidgets.QApplication(sys.argv)<br>
    MainWindow = QtWidgets.QMainWindow()<br>
    ui = Ui_MainWindow()<br>
    ui.setupUi(MainWindow)<br>
    MainWindow.show()<br>
    sys.exit(app.exec_())<br>
  

TypeError:参数1具有意外的类型'QWidget'

1 个答案:

答案 0 :(得分:0)

self.log_in = QtWidgets.QWidget(self.centralwidget)

您将log_in从功能重新定义为QTWidget。当您尝试调用log_in时,它会找到QTWidget,它不是可调用的函数。