0

I have a main file, which handles my PyQt5 UI. I have the program create a new table for each set of data I have, and name it accordingly. The update_status function edits a certain item on a certain table by the ID I gave it.

mainfile.py

class MyUi(object)
    def setupUi(self, MyWindow):...

    def retranslateUi(self, MyWindow):...
    
    # this is what I want to call from another file
    def update_status(self, tid, text):
        # getting task's group for writing to table
        t_file = json.load(open('Data/Tasks/tasks.json', 'r'))

        for group in t_file:
            for task in group['tasks']:
                if task['id'] == tid:
                    task_parent = group['group name']


        for i in self.TaskScreen.children():
            if str(i.__class__.__name__) == 'QTableWidget':
                try:
                    if i.objectName() == task_parent:
                        row = i.rowCount()

                        for r in range(row):
                            task_id = i.item(r, 0).text()
                            if task_id == tid:
                                i.setItem(r, 6, QtWidgets.QTableWidgetItem(str(text)))
                                i.update()
                               
                except UnboundLocalError:
                    print('Item doesnt exist')

When I call update_status from mainfile.py, it works fine.

test.py (the file I need to call update_status from)

from mainfile import MyUi
update_status = MyUi().update_status


update_status(tid='ABC123', text='Test')

This is the traceback I get:

AttributeError: 'MyUi' object has no attribute 'TaskScreen'

I hope I explained this well, Any help would be appreciated.

NikosBeans
  • 127
  • 2
  • 6
  • As the error clearly states, the class `MyUi` is supposed to have `TaskScreen` attribute. Can you show the complete code? – Shiva Mar 07 '21 at 14:37
  • This is one of the many reason for which a warning exists in the header of the file you're trying to modify (which is created by pyuic): those files should **never** be edited (nor you should try to mimic their behavior), as doing so without knowing what you're doing almost always leads to confusion and unexpected behavior. Read more about their correct usage on the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Mar 07 '21 at 15:39
  • By the way, it doesn't work because those files expect a call to `setupUi` with a QWidget as argument, otherwise they do absolutely nothing and are completely empty instances (so the `MyUi` instance you're creating has no `TaskScreen` attribute). And that's another reason for which they shouldn't be used in "other ways" by trying to modify them. Finally, storing an instance method as unique reference (by loosing any reference to the instance itself) is not a very good thing to do. Ah, also, don't use `str(i.__class__.__name__) == 'QTableWidget':` to check for a class, use `isinstance`. – musicamante Mar 07 '21 at 15:42

0 Answers0