M.Lord
M.Lord

Reputation: 101

PyQt5 Variable Creation from For Loop

Instead of doing this:

A = (self.findChild(QtWidgets.QLineEdit, 'A')).text() # Find the input
B = (self.findChild(QtWidgets.QLineEdit, 'B')).text()
C = (self.findChild(QtWidgets.QLineEdit, 'C')).text()

I want to do something like this:

for x in ['A', 'B', 'C']:
   x = (self.findChild(QtWidgets.QLineEdit, 'x')).text()

How do you write more concise code?

Upvotes: 0

Views: 44

Answers (1)

Guimoute
Guimoute

Reputation: 4629

You will have to store the results in a dictionary:

texts = {}
for key in ("A", "B", "C"):
   texts[key] = (self.findChild(QtWidgets.QLineEdit, key)).text()

Afterwards you won't be able to use directly A but texts["A"].

Upvotes: 1

Related Questions