且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 QLineEdit 动态获取文本?

更新时间:2023-11-14 16:32:46

这里有一个完整的例子,如何从 self.aself.b 和将值设置为彼此.也许这个教程也能帮到你.

Here is a complete example how to get the value from self.a and self.b and set the values to each other. Maybe this tutorial helps you, too.

不能使用self.textchangedAself.textchangedB方法的返回值,所以必须使用类的成员变量.

You can not use the return value of the methods self.textchangedA or self.textchangedB, so you have to make use of the member variables of the class.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import sys
from PyQt4 import QtGui

log = logging.getLogger(__name__)


class MyWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)

        vbox = QtGui.QVBoxLayout(self)
        self.setLayout(vbox)

        self.a = QtGui.QLineEdit(self)
        self.b = QtGui.QLineEdit(self)

        vbox.addWidget(self.a)
        vbox.addWidget(self.b)

        self.a.textChanged.connect(self.textchangedA)
        self.b.textChanged.connect(self.textchangedB)

    def textchangedA(self, text):
        log.info("Text from a: %s", text)
        log.info("Text from b: %s", self.b.text())
        # do the processing

    def textchangedB(self, text):
        log.info("Text from b: %s", text)
        log.info("Text from a: %s", self.a.text())



def test():
    app = QtGui.QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    test()