简而言之,我所做的是创建一个包含textEdit字段的自定义窗口小部件。自定义小部件称为FormWidget。
然后在另一个小部件内多次使用此自定义(FormWidget)。我想知道我怎么能得到'文本字段的值,并将其附加到特定变量。
我不想直接从FormWidget小部件本身附加变量,因为我计划多次实例化此小部件,每个文本字段将对应一个不同的变量。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Info goes here...
"""
import sys
from PySide import QtGui, QtCore
# Variables
# ------------------------------------------------------------------------------
namesList = []
# custom name widget
# ------------------------------------------------------------------------------
class FormWidget(QtGui.QWidget):
def __init__(self, title=None):
super(FormWidget, self).__init__()
self.initUI()
if title!=None:
self.listLabel.setText(title)
def initUI(self):
# formatting
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("Input List")
# widgets
self.listLabel = QtGui.QLabel("Label")
self.nameInput = QtGui.QLineEdit()
# signals
self.nameInput.returnPressed.connect(self.pressed_return)
# layout
self.mainLayout = QtGui.QVBoxLayout(self)
self.mainLayout.addWidget(self.listLabel)
self.mainLayout.addWidget(self.nameInput)
self.show()
def pressed_return(self):
txt = self.nameInput.text()
txt = txt.strip()
txt = txt.replace (" ", "_")
self.nameInput.clear()
# Main widget
# ------------------------------------------------------------------------------
class TeamsWidget(QtGui.QWidget):
def __init__(self):
super(TeamsWidget, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.setGeometry(300, 300, 250, 50)
self.setWindowTitle("Names")
# widgets
self.formWidget = FormWidget("Names")
self.btn = QtGui.QPushButton("Print")
# signals
self.btn.clicked.connect(self.printNames)
# layout
self.mainLayout = QtGui.QHBoxLayout(self)
self.mainLayout.addWidget(self.formWidget)
self.mainLayout.addWidget(self.btn)
self.show()
def printNames(self):
print "Printing Names..."
for n in namesList:
print n
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ex = TeamsWidget()
sys.exit(app.exec_())
答案 0 :(得分:1)
只需保留FormWidget中的值列表,将它们存储在returnPressed上。然后向FormWidget添加一个getValue()方法以返回值列表。我还添加了一个clear()方法来初始化和清除值,这是在FormWidget中的init上调用的。
完整的代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Info goes here...
'''
import sys
from PySide import QtGui, QtCore
# Variables
# ------------------------------------------------------------------------------
namesList = []
# custom name widget
# ------------------------------------------------------------------------------
class FormWidget(QtGui.QWidget):
def __init__(self, title=None):
super(FormWidget, self).__init__()
self.clear()
self.initUI()
if title!=None:
self.listLabel.setText(title)
def initUI(self):
# formatting
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Input List')
# widgets
self.listLabel = QtGui.QLabel('Label')
self.nameInput = QtGui.QLineEdit()
# signals
self.nameInput.returnPressed.connect(self.returnPressed)
# layout
self.mainLayout = QtGui.QVBoxLayout(self)
self.mainLayout.addWidget(self.listLabel)
self.mainLayout.addWidget(self.nameInput)
self.show()
def returnPressed(self):
text = self.nameInput.text()
text = text.strip()
text = text.replace (' ', '_')
self.value.append(text)
self.nameInput.clear()
def getValue(self):
return self.value
def clear(self):
self.value = []
# Main widget
# ------------------------------------------------------------------------------
class TeamsWidget(QtGui.QWidget):
def __init__(self):
super(TeamsWidget, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.setGeometry(300, 300, 250, 50)
self.setWindowTitle('Names')
# widgets
self.formWidget = FormWidget('Names')
self.btn = QtGui.QPushButton('Print')
# signals
self.btn.clicked.connect(self.printNames)
# layout
self.mainLayout = QtGui.QHBoxLayout(self)
self.mainLayout.addWidget(self.formWidget)
self.mainLayout.addWidget(self.btn)
self.show()
def printNames(self):
print 'Printing Names...'
for name in self.formWidget.getValue():
print name
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = TeamsWidget()
sys.exit(app.exec_())