是否可以通过在 for 中插入 lineEdit 来缩短 PyQt5 中的代码?

Is it possible to shorten the code in PyQt5 by sticking lineEdit in for?

我有 7 个 lineEdit 块,我希望 textChanged 函数在其中任何一个发生变化时触发,我决定采取简单的方法并按顺序写出来,但现在我想缩短我的代码。

我想缩短这个

        self.lineEdit_2.textChanged.connect(self.textChanged)
        self.lineEdit_3.textChanged.connect(self.textChanged)
        self.lineEdit_4.textChanged.connect(self.textChanged)
        self.lineEdit_5.textChanged.connect(self.textChanged)
        self.lineEdit_6.textChanged.connect(self.textChanged)
        self.lineEdit_7.textChanged.connect(self.textChanged)
        self.lineEdit_8.textChanged.connect(self.textChanged)

像这样

        for n in range(2, 8):  #check changes on the all lineEdit units
            self.lineEdit_n.textChanged.connect(self.textChanged)

或者像这样

        for n in range(2, 8):
            self.lineEdit_n.textChanged.connect(self.textChanged)
            getattr(self, 'lineEdit_n%' % n).textChanged.connect(self.textChanged)

但是没用 感谢您的关注

# Check changes on all lineEdit units
for n in range(2, 9):
    getattr(self, f"lineEdit_{n}").textChanged.connect(self.textChanged)