問題描述
當通過信號連接調用函數時
mybutton.clicked.connect(myfunction)
調用該函數時,其所有參數都設置為 False.即使已設置默認參數.這是預期的行為嗎?
下面的代碼顯示了一個簡單的示例.對于我的特殊情況,將所有參數設置為 False 并不是一個大問題,因為我可以簡單地用 if 語句來捕捉它.但是,我認為了解為什么以這種方式實施它可能對進一步的項目有所幫助.
這個簡單的程序顯示了我所期望的行為.
def 函數(foo=None):打印(富)功能()
<塊引用>
無
但是,在這個最小的 Qt 示例中,函數參數不再是None",而是False".
導入系統從 PyQt5.QtWidgets 導入 QMainWindow、QGridLayout、QWidget、QPushButton、QApplication類主窗口(QMainWindow):def __init__(self):QMainWindow.__init__(self)centralWidget = QWidget(self)self.setCentralWidget(centralWidget)gridLayout = QGridLayout(self)centralWidget.setLayout(gridLayout)btn = QPushButton("按鈕",self)gridLayout.addWidget(btn)btn.clicked.connect(self.function)def 函數(自我,foo=None):打印(富)如果 __name__ == "__main__":應用程序 = QApplication(sys.argv)mainWin = MainWindow()mainWin.show()sys.exit(app.exec_())
<塊引用>
錯誤
我想,問題是:我是否必須忍受在我的函數中沒有可用的默認值(即,一旦我注意到它們被設置為 False,我是否需要重置它們)還是有任何更聰明的方法處理它??/p>
clicked是一個重載信號,即它們是2個同名信號不同簽名.
clicked = QtCore.pyqtSignal([], [bool])# 點擊 = QtCore.pyqtSignal()# clicked = QtCore.pyqtSignal(bool)
有關詳細信息,請閱讀此答案.
建立連接時,PyQt 確定將使用哪個信號,在這種情況下,您的函數的參數類似于第二種形式,因此信號將發送布爾值 False,在您的情況下,它將替換默認值.
如果您不希望這種情況發生,那么您必須使用 @QtCore.pyqtSlot()
裝飾器,以便它不會向您發送任何參數,因此您的函數使用默認參數.
導入系統從 PyQt5 導入 QtCore、QtWidgets類 MainWindow(QtWidgets.QMainWindow):def __init__(self, parent=None):超級(主窗口,自我).__init__(父)centralWidget = QtWidgets.QWidget()self.setCentralWidget(centralWidget)gridLayout = QtWidgets.QGridLayout(centralWidget)btn = QtWidgets.QPushButton("按鈕")gridLayout.addWidget(btn)btn.clicked.connect(self.function)@QtCore.pyqtSlot()def 函數(自我,foo=None):打印(富)如果 __name__ == "__main__":應用程序 = QtWidgets.QApplication(sys.argv)mainWin = MainWindow()mainWin.show()sys.exit(app.exec_())
輸出:
無
When calling a function via a signal connection as in
mybutton.clicked.connect(myfunction)
the function is called with all its arguments set to False. Even though default arguments have been set. Is this the expected behavior?
The code below shows a simple example. For my particular case having all arguments set to False is not a big issue as I can simply catch it with an if statement. However I feel that knowledge on why it is implemented this way may be helpful for further projects.
This simple program shows the behavior I would expect.
def function(foo=None):
print(foo)
function()
None
With this minimal Qt example however, the functions argument is no longer 'None', but 'False'.
import sys
from PyQt5.QtWidgets import QMainWindow, QGridLayout, QWidget, QPushButton, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
btn = QPushButton("button",self)
gridLayout.addWidget(btn)
btn.clicked.connect(self.function)
def function(self, foo=None):
print(foo)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
False
The question, i guess, is: Do I have to live with no having the default values available in my functions (i.e. do I need to reset them once I notice that they were set to False) or is there any smarter way to handle it?
clicked is an overloaded signal, that is, they are 2 signals with the same name and with different signatures.
clicked = QtCore.pyqtSignal([], [bool])
# clicked = QtCore.pyqtSignal()
# clicked = QtCore.pyqtSignal(bool)
For more detail read this answer.
When the connection is made PyQt determines which of the signals will use, in this case the arguments of your function are similar to the second form so the signal will send the boolean False, and in your case it will replace the default value.
If you do not want that to happen then you must force PyQt to use the first form using the @QtCore.pyqtSlot()
decorator so that it does not send you any parameter and therefore your function uses the default parameter.
import sys
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
centralWidget = QtWidgets.QWidget()
self.setCentralWidget(centralWidget)
gridLayout = QtWidgets.QGridLayout(centralWidget)
btn = QtWidgets.QPushButton("button")
gridLayout.addWidget(btn)
btn.clicked.connect(self.function)
@QtCore.pyqtSlot()
def function(self, foo=None):
print(foo)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
Output:
None
這篇關于通過信號調用函數將默認鍵控參數更改為“False".為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!