Python 参数 1 具有意外类型“NoneType”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40982518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Argument 1 has unexpected type 'NoneType'?
提问by Darkdrummer
I have a problem with my PyQt button action. I would like to send a String with the Function but I got this Error:
我的 PyQt 按钮操作有问题。我想发送一个带有函数的字符串,但出现此错误:
TypeError: argument 1 has unexpected type 'NoneType'
类型错误:参数 1 具有意外类型“NoneType”
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QAction
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import *
from PyQt5.uic import *
app = QApplication(sys.argv)
cocktail = loadUi('create.ui')
def mixCocktail(str):
cocktail.show()
cocktail.showFullScreen()
cocktail.lbl_header.setText(str)
widget = loadUi('drinkmixer.ui')
widget.btn_ckt1.clicked.connect(mixCocktail("string"))
widget.show()
sys.exit(app.exec_())
回答by harshil9968
As suggested by user3030010and ekhumoroit expects a callable function. In which case you should replace that argument with lambda: micCocktail("string")
AND ALSOdon't use str
it's a python built-in datatype I have replaced it with _str
正如user3030010和ekhumoro所建议的,它需要一个可调用的函数。在这种情况下,您应该将该参数替换为lambda: micCocktail("string")
AND也不要使用str
它是 python 内置数据类型,我已将其替换为_str
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QAction
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtGui import *
from PyQt5.uic import *
app = QApplication(sys.argv)
cocktail = loadUi('create.ui')
def mixCocktail(_str):
cocktail.show()
cocktail.showFullScreen()
cocktail.lbl_header.setText(_str)
widget = loadUi('drinkmixer.ui')
widget.btn_ckt1.clicked.connect(lambda: micCocktail("string"))
widget.show()
sys.exit(app.exec_())