0

I am starting with the topic of the graphical interface in PyQt5.

I have defined two windows, the first that would be the main one and a sub-window that contains a button, which calls a function to make a graph through its 'clicked' method for the respective event.

My problem is that when executing I get the following error: 'DataInput' object has no attribute 'graph'.

Here I leave the .UI files:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.uic import loadUi

import matplotlib.pyplot as plt

# CLASS MAIN WINDOW 
# ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
class mainWindowInicial(QMainWindow):
    def __init__(self, parent=None):
        super(mainWindowInicial,self).__init__(parent)
        loadUi('Gui/mainwindow.ui', self)
        self.actionData.triggered.connect(self.open_dataInput)      

    # EVENTS ==================================================================
    def open_dataInput(self):
        DataInput(self).show()

def graphics():

    plt.plot((0,5),(0,5))
    plt.show()

class DataInput(QMainWindow):

    def __init__(self, parent=None):
        super(DataInput,self).__init__(parent)
        loadUi('Gui/datawindow.ui', self)
        
        self.pushButton_Graph.clicked.connect(self.graph)
        
        def graph():
            graphics()
        
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = mainWindowInicial()
    widget.show()
    sys.exit(app.exec_())

If anyone understands PyQt5 a bit more, I would appreciate your help, best regards.

Maria
  • 25
  • 2
  • I’m not sure why this is closed since I’m not sure this has anything to do with 'self'. – Macintosh Fan Aug 08 '20 at 20:53
  • 1
    @MacintoshFan It has a lot to do with "self", the OP is unaware that only the methods, attributes, etc. can be invoked through "self", and obviously "graph" is not a DataInput method due to its indentation and that it does not have as a first parameter to "self". Those are python basics that the OP seems to be unaware of. – eyllanesc Aug 08 '20 at 20:56
  • Strange. I can’t help much since I’m only a beginner, but I hope things work from here… – Macintosh Fan Aug 08 '20 at 20:58
  • Also, I don’t think it is a duplicate anyways since it isn’t an issue about 'self', but about PyQt5 it seems... – Macintosh Fan Aug 08 '20 at 20:59
  • Well, I have seen that the question has been redirected, to similar ones. So, please illustrate what the solution would be, to start with it and learn a little more, I provided a script example, @eyllanesc. – Maria Aug 08 '20 at 21:04
  • 1
    @MacintoshFan The problem has nothing to do with PyQt5, the problem is about OOP, not every problem with a code that uses the X library implies that the X library has to do with the problem, it could be caused by the Y library or simply by another cause : syntax problem, logic problem, etc. – eyllanesc Aug 08 '20 at 21:06
  • @Maria The objective of marking as a duplicate is to save both of us time: you because the solution is in the other post so please read it, and me because it saves me from investing my time in another post. Note: analyze my first comment since the cause of the problem is indicated there. – eyllanesc Aug 08 '20 at 21:08
  • I understand, excuse the question, but what do you mean by 'OP'? @eyllanesc – Maria Aug 08 '20 at 21:12
  • @Maria See https://meta.stackoverflow.com/q/253162/6622587 – eyllanesc Aug 08 '20 at 21:13
  • @Maria See also https://meta.stackexchange.com/q/40353/347755 – eyllanesc Aug 08 '20 at 21:16

0 Answers0