0

i'm using Pyqt5 to build a GUI. There is a way to call a function, defined in one class, from another function defined in a second class? As example i have my first class where i define the layout of GUI, and where i placed some buttons and a Canvas where to plot a graph. In the same class i defined the function connected to the button. Anyway, when i press the button i would like to call another function, defined in a second class.

First class

class Window(QMainWindow):
def __init__(self):
    super().__init__()

    self.title = "My_GUI"
    self.top = 100
    self.left = 100
    self.width = 1680
    self. height = 880

    self.InitWindow()

    # Definition of buttons
def InitWindow(self):

    m = PlotCanvas(self, width=5, height=4)
    m.move(300, 50)

    self.btn1 = QPushButton("execute", self)
    self.btn1.setGeometry(20, 410, 150, 50)
    self.btn1.clicked.connect(self.execute) 

def execute(self):
    PlotCanvas.plot(self)

Second class where i define a Canvas to update a plot when the button execute is pressed

class PlotCanvas(FigureCanvas):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
       fig = Figure(figsize=(width, height), dpi=dpi)
       self.axes = fig.add_subplot(111)

       FigureCanvas.__init__(self, fig)
       self.setParent(parent)

       FigureCanvas.setSizePolicy(self,
                               QSizePolicy.Expanding,
                               QSizePolicy.Expanding)
       FigureCanvas.updateGeometry(self)
       # self.plot()

    def plot(self):
        data = [random.random() for i in range(25)]
        ax = self.figure.add_subplot(111)
        ax.plot(data, 'r-')
        self.draw()

When i run the code, python crash. I'm using Pycharm

vittorio
  • 143
  • 3
  • 14
  • 1
    PlotCanvas.plot is not a static function. You should declare the instance as self.m = PlotCanvas() and then call m.plot() instead – Luv Apr 28 '20 at 13:11
  • Thank you but could you explain me better this procedure? Where i have to declare the instance self.m = PlotCanvas and thatn call m.plot()? – vittorio Apr 28 '20 at 13:17
  • Understand. Thank you! Not it works – vittorio Apr 28 '20 at 13:19
  • 1
    minor edit, no need to add self when calling plot. If that works would you please accept the answer. – Luv Apr 28 '20 at 13:52

1 Answers1

1

To access functions that are not class methods, you need to create an instance of the class and call the method via the object.

Try this

class Window(QMainWindow):
    def __init__(self):
        super().__init__() 
        self.title = "My_GUI"
        self.top = 100
        self.left = 100
        self.width = 1680
        self. height = 880
        self.InitWindow()
        # Definition of buttons

    def InitWindow(self):
        self.m = PlotCanvas(self, width=5, height=4)
        self.m.move(300, 50)    
        self.btn1 = QPushButton("execute", self)
        self.btn1.setGeometry(20, 410, 150, 50)
        self.btn1.clicked.connect(self.execute) 

    def execute(self):
        self.m.plot()
Luv
  • 386
  • 4
  • 15