我的PyQt按钮操作有问题。我想发送一个带有函数的字符串,但是我收到了这个错误:
TypeError:参数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_())
答案 0 :(得分:9)
根据user3030010和ekhumoro的建议,它需要一个可调用的函数。在这种情况下,您应该用lambda: micCocktail("string")
替换该参数
AND ALSO 不要使用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_())
答案 1 :(得分:0)
代替这个
<块引用>widget.btn_ckt1.clicked.connect(mixCocktail("string"))
写
<块引用>widget.btn_ckt1.clicked.connect(lambda:mixCocktail("string"))