Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - Argument 1 has unexpected type 'NoneType'?

I have a problem with my PyQt button action. I would like to send a String with the Function but I got this Error:

TypeError: argument 1 has unexpected type '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_())
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As suggested by user3030010 and ekhumoro it expects a callable function. In which case you should replace that argument with lambda: mixCocktail("string") AND ALSO don't use str it's a python built-in datatype I have replaced it with _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: mixCocktail("string"))

widget.show()
sys.exit(app.exec_())

More about lambda functions: What is a lambda (function)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...