下面是一个完整的示例,如何从self.a和self.b中获取值并将这些值相互设置。也许这个tutorial对你也有帮助。在
不能使用方法self.textchangedA或{}的返回值,因此必须使用类的成员变量。在#!/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()