有些时候需要创建尽可能多的小部件(例如QtGui.QLineEdit()),因为列表中有值(我们不知道列表变量中存储了多少个值)。 我可以创建一个循环函数来运行多次,就像存储在列表中的值一样......例如:
for each in myList:
myLineEdit = QtGui.QLineEdit("myLineEdit")
这种方法的问题在于,每个循环都声明了相同的变量名。稍后将无法访问myLineEdit变量。 我听说有些人使用eval()成功了吗?还是exec()?功能。看一个例子会很有趣。如果有其他方法可以做到,请发布。以下是开始时的示例代码(如果您愿意):
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
mainQWidget = QtGui.QWidget()
mainLayout=QtGui.QVBoxLayout()
for i in range(5):
exec( 'myGroupBox'+str(i)+'= QtGui.QGroupBox() ' )
exec( 'myLayout'+str(i)+' = QtGui.QHBoxLayout()' )
exec( 'label'+str(i)+'=QtGui.QLabel("Name '+str(i)+': ")' )
exec( 'self.myLineEdit'+str(i)+'=QtGui.QLineEdit()' )
exec( 'myLayout'+str(i)+'.addWidget(label'+str(i)+')' )
exec( 'myLayout'+str(i)+'.addWidget(self.myLineEdit'+str(i)+', QtCore.Qt.AlignRight)' )
exec( 'myGroupBox'+str(i)+'.setLayout(myLayout'+str(i)+')' )
exec( 'mainLayout.addWidget(myGroupBox'+str(i)+')' )
mainQWidget.setLayout(mainLayout)
self.setCentralWidget(mainQWidget)
window = MainWindow()
window.show()
window.resize(480,320)
sys.exit(app.exec_())
from PyQt4 import QtCore, QtGui
import random
app = QtGui.QApplication(sys.argv)
class MainWindow(QtGui.QMainWindow):
def __init__(self, argList):
super(MainWindow, self).__init__()
self.argList=argList
mainQWidget = QtGui.QWidget()
mainLayout=QtGui.QVBoxLayout()
for i in range(len(self.argList)):
exec( 'myGroupBox'+str(i)+'= QtGui.QGroupBox() ' )
exec( 'myLayout'+str(i)+' = QtGui.QHBoxLayout()' )
exec( 'label'+str(i)+'=QtGui.QLabel("Name '+str(self.argList[i])+': ")' )
exec( 'label'+str(i)+'.setFixedWidth(100)' )
exec( 'self.myLineEdit'+str(i)+'=QtGui.QLineEdit()' )
exec( 'self.myLineEdit'+str(i)+'.setText("'+str(random.random())+'")' )
exec( 'myLayout'+str(i)+'.addWidget(label'+str(i)+')' )
exec( 'myLayout'+str(i)+'.addWidget(self.myLineEdit'+str(i)+', QtCore.Qt.AlignRight)' )
exec( 'myGroupBox'+str(i)+'.setLayout(myLayout'+str(i)+')' )
exec( 'mainLayout.addWidget(myGroupBox'+str(i)+')' )
ButtonBox = QtGui.QGroupBox()
ButtonsLayout = QtGui.QHBoxLayout()
Button_01 = QtGui.QPushButton("Close")
Button_01.clicked.connect(self.close)
Button_02 = QtGui.QPushButton("Print")
Button_02.clicked.connect(self.printOut)
ButtonsLayout.addWidget(Button_01)
ButtonsLayout.addWidget(Button_02)
ButtonBox.setLayout(ButtonsLayout)
mainLayout.addWidget(ButtonBox)
mainQWidget.setLayout(mainLayout)
self.setCentralWidget(mainQWidget)
def printOut(self):
for i in range(len(self.argList)):
exec( 'print self.myLineEdit'+str(i)+'.text()' )
def close(self):
sys.exit()
myList=['One','Two','Tree','Four','Five','Six','Seven']
window = MainWindow(myList)
window.show()
window.resize(480,320)
sys.exit(app.exec_())
答案 0 :(得分:7)
处理这种情况的一种简单方法是使用列表:
lineEdits = []
for _ in range(5):
myLineEdit = QtGui.QLineEdit("myLineEdit")
lineEdits.append(myLineEdit)
myLayout = QtGui.QHBoxLayout()
for lineEdit in lineEdits:
myLayout.addWidget(lineEdit)
或者你可以使用字典,如果你以后想用密钥引用一个小部件:
lineEdits = {}
for i in range(5):
myLineEdit = QtGui.QLineEdit("myLineEdit")
lineEdits[i] = myLineEdit
lineEdits[3].setText("My new text")