0

I'm having a problem where I'm unable to access PyQT QTextEdits and QLineEdits from a separate class method. I'm getting different variations of this error:

AttributeError: type object 'MyApp' has no attribute 'Ui_MainWindow'

I've tried everything that I can think of, including:

  • Using a child-class
  • Explicitly adding the GUI class as an argument in the non-child class
  • Calling on the items by using 'self..text()'
  • Calling on the items by using 'MyApp..text()' (GUI inhereted class name)
  • Calling on the items by using 'MyApp.Ui_MainWindow..text()' (GUI inhereted class name)
  • Calling on the items by using 'MyApp.Ui_MainWindow.setupUi..text()' (GUI inhereted class name)
  • Calling on items by using 'UiMainWindow..text()' (imported GUI .py file class)
  • Calling on items by using 'UiMainWindow.setupUi..text()' (imported GUI .py file class + method)
  • Calling on items by using 'UiMainWindow.setupUi..text()' (imported GUI .py file class + method)

Please see my code below:

from app import Ui_MainWindow #import UI


class MyApp(QtWidgets.QMainWindow, Ui_MainWindow): #GUI class
  def __init__(self):
    QtWidgets.QMainWindow.__init__(self)
    Ui_MainWindow.__init__(self)

    self.button_search_item.clicked.connect(new_class.get_information)

class new_class(MyApp): #new class that i want to call on qtext and qline edits.
def get_information(self):
    print(str(MyApp.Ui_MainWindow.setupUi.qlineedit_item.text()))


#OUTPUT: 
#AttributeError: type object 'MyApp' has no attribute 'Ui_MainWindow'
Gregory
  • 315
  • 1
  • 3
  • 13
  • use `print(str(self.qlineedit_item.text()))` and move `self.button_search_item.clicked.connect(new_class.get_information)` to newclass constructor: `self.button_search_item.clicked.connect(self.get_information)` – eyllanesc Jan 11 '19 at 15:36
  • It seems that you do not understand the concept of inheritance, the children can use the things that he inherited from the parent, not the parent's things, but the parent can not access the children, he can point out some implementations through the abstract methods. On the other hand, when you inherit, you can access the things that you inherit without asking the parent for anything, in your case it is not necessary to access through the MyApp but through the self. – eyllanesc Jan 11 '19 at 15:41
  • So, as a recommendation, review your OOP concepts, and especially the inheritance part, which is your inheritance objective, what advantages and disadvantages, etc. – eyllanesc Jan 11 '19 at 15:42
  • Hey @eyllanesc! First of all, thank you for your detailed response! I've moved the button_clicked connection to the new_class constructor, but it doesn't seem to be working. No errors, but nothing is returned, even when I use plain print('I AM WORKING') text as the action. Regarding inheritance, you are absolutely correct. I am still learning OOP, and I find it a bit difficult, but I am reading through the links you provided-- thank you for those! – Gregory Jan 11 '19 at 16:23
  • I think that it has incorrectly implemented what I have indicated since it does not show the code, besides that the code that it currently provides is not a [mcve], on the other hand I do not see you call the function setupUi(), please read the docs : http://pyqt.sourceforge.net/Docs/PyQt5/designer.html and in the following link is my test code: https://gist.github.com/eyllanesc/d5d83e71d2eb6e6f94f027b2c50d19d7 – eyllanesc Jan 11 '19 at 16:31
  • On the other hand to use Qt you have to have a solid knowledge about OOP, it is in the nature of Qt so if you do not have it it will be a headache, so before trying to learn Qt I recommend you to consolidate your knowledge of OOP – eyllanesc Jan 11 '19 at 16:36
  • Hey @eyllanesc, a couple updated: Ah, you are correct-- I missed the setupUi line from Myapp on my previous post! And thank you for the GitHub link; I didn't set the app to my child class. I fixed that and it worked! Regarding the OOP principles, I have begun reading up on them more seriously, particularly multiple inheritance as it relates to Python. Unfortunately moving away from pyqt is not an option. – Gregory Jan 12 '19 at 13:09

0 Answers0