如何动态读取 QLineEdit 数据
How to Read QLineEdit data dynamically
我正在尝试在 PyQt5 中动态创建标签和文本框,但是,我不知道如何在用户点击保存按钮时读取输入到文本框中的数据。我的代码如下所示:
self.setWindowTitle("Yaml --> Json")
self.setGeometry(self.left, self.top, self.width, self.height)
self.createLayout()
vbox = QVBoxLayout()
for i in range(0, len(self.listItems)):
vbox.addWidget(QLabel(list(self.listItems.keys())[i]))
vbox.addWidget(QLineEdit())
vbox.addWidget(self.groupBox)
self.setLayout(vbox)
self.show()
def createLayout(self):
self.groupBox = QGroupBox()
hboxLayout = QHBoxLayout()
button = QPushButton("Save", self)
button.setIcon(QtGui.QIcon("save.png"))
button.setIconSize(QtCore.QSize(40, 40))
button.setMinimumHeight(40)
button.clicked.connect(self.ClickSave)
hboxLayout.addWidget(button)
button1 = QPushButton("Exit", self)
button1.setIcon(QtGui.QIcon("exit.png"))
button1.setIconSize(QtCore.QSize(40, 40))
button1.setMinimumHeight(40)
button1.clicked.connect(self.ClickExit)
hboxLayout.addWidget(button1)
self.groupBox.setLayout(hboxLayout)
def ClickExit(self):
print("Exited!!")
sys.exit()
def ClickSave(self):
print("Saved!")```
您可以将稍后要访问的小部件分配给实例变量或将它们存储在列表中,例如
self.line_edit_list = []
for i in range(0, len(self.listItems)):
vbox.addWidget(QLabel(list(self.listItems.keys())[i]))
line_edit = QLineEdit()
vbox.addWidget(line_edit)
self.line_edit_list.append(line_edit)
....
def ClickSave(self):
for edit in self.line_edit_list:
print(edit.text())
我正在尝试在 PyQt5 中动态创建标签和文本框,但是,我不知道如何在用户点击保存按钮时读取输入到文本框中的数据。我的代码如下所示:
self.setWindowTitle("Yaml --> Json")
self.setGeometry(self.left, self.top, self.width, self.height)
self.createLayout()
vbox = QVBoxLayout()
for i in range(0, len(self.listItems)):
vbox.addWidget(QLabel(list(self.listItems.keys())[i]))
vbox.addWidget(QLineEdit())
vbox.addWidget(self.groupBox)
self.setLayout(vbox)
self.show()
def createLayout(self):
self.groupBox = QGroupBox()
hboxLayout = QHBoxLayout()
button = QPushButton("Save", self)
button.setIcon(QtGui.QIcon("save.png"))
button.setIconSize(QtCore.QSize(40, 40))
button.setMinimumHeight(40)
button.clicked.connect(self.ClickSave)
hboxLayout.addWidget(button)
button1 = QPushButton("Exit", self)
button1.setIcon(QtGui.QIcon("exit.png"))
button1.setIconSize(QtCore.QSize(40, 40))
button1.setMinimumHeight(40)
button1.clicked.connect(self.ClickExit)
hboxLayout.addWidget(button1)
self.groupBox.setLayout(hboxLayout)
def ClickExit(self):
print("Exited!!")
sys.exit()
def ClickSave(self):
print("Saved!")```
您可以将稍后要访问的小部件分配给实例变量或将它们存储在列表中,例如
self.line_edit_list = []
for i in range(0, len(self.listItems)):
vbox.addWidget(QLabel(list(self.listItems.keys())[i]))
line_edit = QLineEdit()
vbox.addWidget(line_edit)
self.line_edit_list.append(line_edit)
....
def ClickSave(self):
for edit in self.line_edit_list:
print(edit.text())