PyQt QLineEdit textchanged事件

参考https://stackoverflow.com/questions/12182133/pyqt4-combine-textchanged-and-editingfinished-for-qlineedit

class myLineEdit(QLineEdit):
    signal=pyqtSignal(str)
    def __init__(self,parent):
        super(myLineEdit, self).__init__(parent)
        #绑定textchanged事件
        self.textChanged.connect(self.handleTextChanged)

    def handleTextChanged(self):
        self.signal.emit(self.text())
class myWin(QWidget):
    def __init__(self):
        super(myWin,self).__init__()
        self.line=myLineEdit(self)
        self.line.signal.connect(self.text_changed)
    def text_changed(self,text):
        print(text)

方法二:

class myWin(QWidget):
    def __init__(self):
        super(myWin,self).__init__()
        self.line=QLineEdit(self)
        self.line.textChanged.connect(self.text_changed)
        #self.line.editFinished.connect(self.text_finished)
    def text_changed(self,text):
        print(self.line.text())
    def text_finished(self):
        print(self.line.text())

你可能感兴趣的:(PyQt5,Python)