将默认参数和自定义参数传递给slot函数

2024-06-28 11:02:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在PyQt5和Python3.6中使用信号/插槽机制。你知道吗

我知道如何检索(在slot函数中)链接到发出信号的“default”参数:

self.myQLineEdit.textEdited.connect(self.my_slot_function)
def my_slot_function(self, text: str) {
    print(text)
}

我还知道如何将自定义参数发送到我的slot函数:

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))
}

Tags: 函数textselfdefault参数parammydef
1条回答
网友
1楼 · 发布于 2024-06-28 11:02:26

试试看:

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_())

enter image description here

相关问题 更多 >