0

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 :).

Scott Ulmer
  • 139
  • 11
  • 4
    Why not just stick them in a list or dictionary? – Carcigenicate Nov 12 '19 at 19:17
  • Can you give me example of how you would implement that? I know how to loop through lists, but I am not sure how I would insert the variable names into the UI element names as it is a name of a variable. I am trying to nest variables to create variable names, that is the part I am stuck on. – Scott Ulmer Nov 12 '19 at 19:19
  • 1
    `elements = {"name": (the_label, the_textEdit)}`. Then `elements["name"]` to get the label/edit pair. I have it as a tuple of elements. It could also be a list, or some other structure depending on the circumstances. – Carcigenicate Nov 12 '19 at 19:21
  • So how do I insert that into another variable name? For example it would be self.name[0].setText()? – Scott Ulmer Nov 12 '19 at 19:24
  • @ScottUlmer you **really** should not be using dynamic variables like this. There are ways, but again, almost certainly, there is a better way to accmoplish whatever it is you are trying to accomplish, likely by just using a *container* like a `list` or a `dict` – juanpa.arrivillaga Nov 12 '19 at 19:25
  • @ScottUlmer `elements["name"][0].setText()`. The inner tuples could be `NamedTuple`s too, then you can refer to each by a name instead of `[0]` and `[1]`. And to add another entry: `elements.put("new name", (new_label, new_edit))`. – Carcigenicate Nov 12 '19 at 19:31
  • Sorry, I must be very dense... I can't figure out how to get it to work. I have: list = ["date", "memo"] for item in range(len(list)): print(item) self.list[item]_edit.setText("hey") This is invalid syntax. – Scott Ulmer Nov 12 '19 at 20:18
  • @ScottUlmer Why do you have `_edit` in there? – Carcigenicate Nov 12 '19 at 20:25
  • because I have a self.date_label (that says "Date") and is a label and a self.date_edit which is a textEdit in PyQt5 (for user input) – Scott Ulmer Nov 12 '19 at 20:27
  • @ScottUlmer "date_edit" would be, with the code I presented in my comments above, `elements["date"][1]`. Or again, you could use a named tuple instead of a normal tuple if you want to refer to the second field as `edit` instead of `[1]`. – Carcigenicate Nov 12 '19 at 20:27
  • Sorry I keep bugging you, I am getting an error still: AttributeError: 'Ui' object has no attribute 'my_list'...

    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
  • @ScottUlmer That error is becuase you have `self.my_list`, but `my_list` isn't a member of the object judging my your definition of it. And when I get home I'll try to write up a gist. – Carcigenicate Nov 12 '19 at 20:45

1 Answers1

1

Perhaps you can get them from the object's dictionary of variables and assign them to new variable names?

var = 'test'

d = self.__dict__
label = d.get(f'{var}_label')
text_edit = d.get(f'{var}_textEdit')
if label:
    label.text("Name?")
if text_edit:
    text_edit.setText("")
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • I tried that and I was getting "None" , I tried to change it a little and I got "AttributeError: 'Ui' object has no attribute 'label'" – Scott Ulmer Nov 12 '19 at 20:07
  • `if label:` is meant to guard against the possibility that the variable is not in `locals`. Perhaps it is in your `globals`? Are you sure that the object exists? – Alexander Nov 12 '19 at 20:17
  • So I did a simple print("here") inside the if label, and it never printed. I think the issue is that it is inside a class. The correct name for the label is self.date_label, and the correct code would be self.date_label.setText("") – Scott Ulmer Nov 12 '19 at 20:24
  • 1
    Then instead of `local()`, use `self.__dict__`. – Alexander Nov 12 '19 at 20:48
  • You are a genius, thank you SO much :D. – Scott Ulmer Nov 12 '19 at 20:51