python程序出现TypeError: 'NoneType'

最近想自学一下用python做UI,但是根据网上的教程,在.ui文件转换为.py后,无法调用自定义的函数ShowButton-click,请大大看一下

-*- coding: utf-8 -*-

Form implementation generated from reading ui file 'test_login_form.ui'

#

Created by: PyQt5 UI code generator 5.9

#

WARNING! All changes made in this file will be lost!

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication , QMainWindow
from test_login import *

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(140, 100, 491, 321))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.CloseButton = QtWidgets.QPushButton(self.gridLayoutWidget)
self.CloseButton.setObjectName("CloseButton")
self.verticalLayout.addWidget(self.CloseButton)
self.ClearButton = QtWidgets.QPushButton(self.gridLayoutWidget)
self.ClearButton.setObjectName("ClearButton")
self.verticalLayout.addWidget(self.ClearButton)
self.lineEdit = QtWidgets.QLineEdit(self.gridLayoutWidget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.ShowButton = QtWidgets.QPushButton(self.gridLayoutWidget)
self.ShowButton.setObjectName("ShowButton")
self.horizontalLayout.addWidget(self.ShowButton)
self.ShowFormButton = QtWidgets.QPushButton(self.gridLayoutWidget)
self.ShowFormButton.setObjectName("ShowFormButton")
self.horizontalLayout.addWidget(self.ShowFormButton)
self.gridLayout.addLayout(self.horizontalLayout, 0, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    self.ClearButton.clicked.connect(self.lineEdit.clear)
    self.CloseButton.clicked.connect(MainWindow.close)
    self.ShowButton.clicked.connect(self.ShowButton_click())
    self.ShowFormButton.clicked.connect(self.ShowFromButton_click())
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.CloseButton.setText(_translate("MainWindow", "Close"))
    self.ClearButton.setText(_translate("MainWindow", "ClearEdit"))
    self.ShowButton.setText(_translate("MainWindow", "这是一个用户登录界面"))
    self.ShowFormButton.setText(_translate("MainWindow", "登录"))

def ShowButton_click(self):
    QtWidgets.QMessageBox.information(self.ShowButton, "标题", "这是第一个PyQt5 GUI程序")

def ShowFromButton_click(slef):
    app = QApplication(sys.argv)
    loginWindow = QMainWindow()
    Ui_LoginWindow().setupUi(loginWindow)
    loginWindow.show()
    sys.exit(app.exec_())

if name == '__main__':
app = QApplication(sys.argv)
mainWindow = QMainWindow()
Ui_MainWindow().setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
pass


Connected to pydev debugger (build 172.4343.24)
Traceback (most recent call last):
File "D:\PyCham\PyCharm Community Edition 2017.2.4\helpers\pydev\pydevd.py", line 1599, in
globals = debugger.run(setup['file'], None, None, is_module)
File "D:\PyCham\PyCharm Community Edition 2017.2.4\helpers\pydev\pydevd.py", line 1026, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\PyCham\PyCharm Community Edition 2017.2.4\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:\Python36\Project\test_login_form.py", line 84, in
Ui_MainWindow().setupUi(mainWindow)
File "D:\Python36\Project\test_login_form.py", line 59, in setupUi
self.ShowButton.clicked.connect(self.ShowButton_click())
TypeError: argument 1 has unexpected type 'NoneType'

Process finished with exit code 1

1个回答

File "D:\Python36\Project\test_login_form.py", line 59, in setupUi
self.ShowButton.clicked.connect(self.ShowButton_click())
TypeError: argument 1 has unexpected type 'NoneType' 

看这两句的意思是调用self.ShowButton.clicked.connect()这个函数的时候输入的参数是NoneType,而你作为参数传进去的self.ShowButton_click()并没有返回值,是不是这里导致了问题呢

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐