sorry that this question will be poorly worded as I do not even know the term used to Google it to even get a real valid question formed.
In short I have a lot of UI elements with the same naming convention and instead of listing hundreds of lines of code, I want to make a simply for loop (range of a list) to populate the information.
My example
var = "name"
{var}_label.text("Name?")
{var}_textEdit.setText("")
I would like something like that and it would know that it would be (in this example) name_label.text and name_textEdit. Is there a way to do this?
A real example (not full working code, but you can see the issue):
if self.date_edit.text() == "":
QMessageBox.warning(self, "Warning", f"You need to fill out an date header.")
return
In this example the "date" should be able to be populated in both the UI variable name and the message box text.
Code that I have tried:
my_list = ["date_edit", "date_label"]
for item in range(len(my_list)):
print(item)
self.my_list[item].setText("hey")
Error: AttributeError: 'Ui' object has no attribute 'my_list'
however there is a self.date_edit
Final example of working code:
my_list = ["date"]
for item in range(len(my_list)):
d = self.__dict__
label = d.get(f'{my_list[item]}_label')
text_edit = d.get(f'{my_list[item]}_edit')
if label:
label.setText("Name?")
if text_edit:
text_edit.setText("WOW")
Thank you everyone for your help :).
code is: my_list = ["date_edit", "date_label"] for item in range(len(my_list)): print(item) self.my_list[item].setText("hey")
Can you add a quick full example of your code? just a list of two items and have it work?
– Scott Ulmer Nov 12 '19 at 20:34