Python 将默认参数和自定义参数传递给slot函数
我在PyQt5和Python3.6中使用信号/插槽机制 我知道如何检索(在slot函数中)链接到发出信号的“default”参数:Python 将默认参数和自定义参数传递给slot函数,python,pyqt,Python,Pyqt,我在PyQt5和Python3.6中使用信号/插槽机制 我知道如何检索(在slot函数中)链接到发出信号的“default”参数: self.myQLineEdit.textEdited.connect(self.my_slot_function) def my_slot_function(self, text: str) { print(text) } 我还知道如何向插槽函数发送自定义参数: my_param = 123 self.myQLineEdit.textEdited.con
self.myQLineEdit.textEdited.connect(self.my_slot_function)
def my_slot_function(self, text: str) {
print(text)
}
我还知道如何向插槽函数发送自定义参数:
my_param = 123
self.myQLineEdit.textEdited.connect(lambda: self.my_slot_function(my_param))
def my_slot_function(self, param: int) {
print(str(param))
}
但是我不知道如何在保留原始“default”参数的同时发送自定义参数
可能是这样的:
my_param = 123
self.myQLineEdit.textEdited.connect(lambda: self.my_slot_function(default, my_param))
def my_slot_function(self, text: str, param: int) {
print(text)
print(str(param))
}
试试看:
import sys
from PyQt5.QtWidgets import *
class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
my_param = 123
self.myQLineEdit = QLineEdit()
self.myQLineEdit.textEdited.connect(
lambda default, my_param=my_param: self.my_slot_function(default, my_param)
)
lay = QVBoxLayout(self)
lay.addWidget(self.myQLineEdit)
def my_slot_function(self, text: str, param: int):
print(text)
print(str(param))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
这个“重复”问题的答案符合我的需要,但“重复”问题本身并不清楚,我无法找到它。