Home>

I'm not quite sure how to link a separate widget in a windowed application created in Qt Designer using PyQt5 to a button.

The task is that when the button is pressed, graphs are drawn (already on the created axes in a separate widget) using the entered values ​​in the fields in the same application. When the button was pressed again, the graphs were rendered again.

Code generated by Qt Designer :

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import numpy as np
class Ui_MainWindow (object):
    def setupUi (self, MainWindow):
        MainWindow.setObjectName ("MainWindow")
        MainWindow.resize (785, 594)
        self.centralwidget= QtWidgets.QWidget (MainWindow)
        self.centralwidget.setObjectName ("centralwidget")
        self.inoutA1= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutA1.setGeometry (QtCore.QRect (30, 10, 101, 31))
        self.inoutA1.setObjectName ("inoutA1")
        self.inoutf1= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutf1.setGeometry (QtCore.QRect (30, 50, 101, 31))
        self.inoutf1.setObjectName ("inoutf1")
        self.inoutphi1= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutphi1.setGeometry (QtCore.QRect (30, 90, 101, 31))
        self.inoutphi1.setObjectName ("inoutphi1")
        self.plainTextEdit_4= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.plainTextEdit_4.setGeometry (QtCore.QRect (170, 90, 104, 31))
        self.plainTextEdit_4.setObjectName ("plainTextEdit_4")
        self.inoutphi2= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutphi2.setGeometry (QtCore.QRect (170, 90, 104, 31))
        self.inoutphi2.setObjectName ("inoutphi2")
        self.inoutf2= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutf2.setGeometry (QtCore.QRect (170, 50, 104, 31))
        self.inoutf2.setObjectName ("inoutf2")
        self.inoutA2= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutA2.setGeometry (QtCore.QRect (170, 10, 104, 31))
        self.inoutA2.setObjectName ("inoutA2")
        self.inoutphi3= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutphi3.setGeometry (QtCore.QRect (310, 90, 101, 31))
        self.inoutphi3.setObjectName ("inoutphi3")
        self.inoutf3= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutf3.setGeometry (QtCore.QRect (310, 50, 101, 31))
        self.inoutf3.setObjectName ("inoutf3")
        self.inoutA3= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutA3.setGeometry (QtCore.QRect (310, 10, 101, 31))
        self.inoutA3.setObjectName ("inoutA3")
        self.label= QtWidgets.QLabel (self.centralwidget)
        self.label.setGeometry (QtCore.QRect (10, 10, 16, 21))
        self.label.setObjectName ("label")
        self.label_2= QtWidgets.QLabel (self.centralwidget)
        self.label_2.setGeometry (QtCore.QRect (150, 10, 16, 21))
        self.label_2.setObjectName ("label_2")
        self.label_3= QtWidgets.QLabel (self.centralwidget)
        self.label_3.setGeometry (QtCore.QRect (290, 10, 16, 21))
        self.label_3.setObjectName ("label_3")
        self.label_4= QtWidgets.QLabel (self.centralwidget)
        self.label_4.setGeometry (QtCore.QRect (10, 50, 16, 21))
        self.label_4.setObjectName ("label_4")
        self.label_5= QtWidgets.QLabel (self.centralwidget)
        self.label_5.setGeometry (QtCore.QRect (5, 90, 21, 21))
        self.label_5.setObjectName ("label_5")
        self.label_6= QtWidgets.QLabel (self.centralwidget)
        self.label_6.setGeometry (QtCore.QRect (150, 50, 16, 21))
        self.label_6.setObjectName ("label_6")
        self.label_7= QtWidgets.QLabel (self.centralwidget)self.label_7.setGeometry (QtCore.QRect (145, 90, 21, 21)) self.label_7.setObjectName ("label_7")
        self.label_8= QtWidgets.QLabel (self.centralwidget)
        self.label_8.setGeometry (QtCore.QRect (285, 90, 21, 21))
        self.label_8.setObjectName ("label_8")
        self.label_9= QtWidgets.QLabel (self.centralwidget)
        self.label_9.setGeometry (QtCore.QRect (290, 50, 16, 21))
        self.label_9.setObjectName ("label_9")
        self.inoutfd= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutfd.setGeometry (QtCore.QRect (30, 130, 101, 31))
        self.inoutfd.setObjectName ("inoutfd")
        self.label_10= QtWidgets.QLabel (self.centralwidget)
        self.label_10.setGeometry (QtCore.QRect (10, 130, 21, 20))
        self.label_10.setObjectName ("label_10")
        self.inoutN= QtWidgets.QPlainTextEdit (self.centralwidget)
        self.inoutN.setGeometry (QtCore.QRect (170, 130, 104, 31))
        self.inoutN.setObjectName ("inoutN")
        self.label_11= QtWidgets.QLabel (self.centralwidget)
        self.label_11.setGeometry (QtCore.QRect (150, 130, 16, 21))
        self.label_11.setObjectName ("label_11")
        self.btnplot= QtWidgets.QPushButton (self.centralwidget)
        self.btnplot.setGeometry (QtCore.QRect (310, 130, 101, 31))
        self.btnplot.setObjectName ("btnplot")
        self.widget= QtWidgets.QWidget (self.centralwidget)
        self.widget.setGeometry (QtCore.QRect (9, 169, 771, 371))
        self.widget.setObjectName ("widget")
        MainWindow.setCentralWidget (self.centralwidget)
        self.menubar= QtWidgets.QMenuBar (MainWindow)
        self.menubar.setGeometry (QtCore.QRect (0, 0, 785, 21))
        self.menubar.setObjectName ("menubar")
        MainWindow.setMenuBar (self.menubar)
        self.statusbar= QtWidgets.QStatusBar (MainWindow)
        self.statusbar.setObjectName ("statusbar")
        MainWindow.setStatusBar (self.statusbar)
        self.toolBar= QtWidgets.QToolBar (MainWindow)
        self.toolBar.setObjectName ("toolBar")
        MainWindow.addToolBar (QtCore.Qt.TopToolBarArea, self.toolBar)
        self.retranslateUi (MainWindow)
        QtCore.QMetaObject.connectSlotsByName (MainWindow)

The widget in which the charts should be drawn is indicated above under the name widget

Code with a function that draws the same graphs:

import matplotlib.pyplot as plt
import numpy as np
def plot_graph (A1, A2, A3, f1, f2, f3, phi1, phi2, phi3, fd, N):
    tstep= 1 /fd
    t= np.linspace (0, (N-1) * tstep, N)
    fstep= fd /N
    f= np.linspace (0, (N-1) * fstep, N)
    y= A1 * np.sin (2 * np.pi * f1 * tstep * t + phi1) + A2 * np.sin (2 * np.pi * f2 * tstep * t + phi2) + A3 * np.sin ( 2 * np.pi * f3 * tstep * t + phi3)
    X= np.fft.fft (y) #X -will be a series of complex numbers
    X_mag= np.abs (X) /N # Normalize X
    f_plot= f [0: int (N /2 + 1)]
    X_mag_plot= 2 * X_mag [0: int (N /2 + 1)]
    X_mag_plot [0]= X_mag_plot [0] /2
    # //////////////////////
    fig, ax= plt.subplots (1, 2, figsize= (4, 2))
    ax [0] .set (xlabel= 'time, s', ylabel= 'y', title= 'Signal')
    ax [1] .set (xlabel= 'freq, Hz', ylabel= 'y', title= 'Frequency spectrum')
    ax [0] .plot (t, y, '.-')
    ax [1] .plot (f_plot, X_mag_plot, '.-')
    ax [0] .grid ()
    ax [1] .grid ()
    fig.savefig ("test.png")
    plt.show ()
plot_graph ()
  • Answer # 1

    Here is your implementation with plotting in a separate window.

    If you want to place your charts in the main application window, then see how it might look.

    Then, if you need to, try your own implementation.

    If something does not work out -ask a new question and please, your example should be minimally reproducible...

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QApplication, QMainWindow
    from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
    import matplotlib.pyplot as plt
    import numpy as np
    class Ui_MainWindow (object):
        def setupUi (self, MainWindow):
            MainWindow.setObjectName ("MainWindow")
            MainWindow.resize (785, 594)
            self.centralwidget= QtWidgets.QWidget (MainWindow)
            self.centralwidget.setObjectName ("centralwidget")
            self.inoutA1= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutA1.setGeometry (QtCore.QRect (30, 10, 101, 31))
            self.inoutA1.setObjectName ("inoutA1")
            self.inoutf1= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutf1.setGeometry (QtCore.QRect (30, 50, 101, 31))
            self.inoutf1.setObjectName ("inoutf1")
            self.inoutphi1= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutphi1.setGeometry (QtCore.QRect (30, 90, 101, 31))
            self.inoutphi1.setObjectName ("inoutphi1")
            self.plainTextEdit_4= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.plainTextEdit_4.setGeometry (QtCore.QRect (170, 90, 104, 31))
            self.plainTextEdit_4.setObjectName ("plainTextEdit_4")
            self.inoutphi2= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutphi2.setGeometry (QtCore.QRect (170, 90, 104, 31))
            self.inoutphi2.setObjectName ("inoutphi2")
            self.inoutf2= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutf2.setGeometry (QtCore.QRect (170, 50, 104, 31))
            self.inoutf2.setObjectName ("inoutf2")
            self.inoutA2= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutA2.setGeometry (QtCore.QRect (170, 10, 104, 31))
            self.inoutA2.setObjectName ("inoutA2")
            self.inoutphi3= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutphi3.setGeometry (QtCore.QRect (310, 90, 101, 31))
            self.inoutphi3.setObjectName ("inoutphi3")
            self.inoutf3= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutf3.setGeometry (QtCore.QRect (310, 50, 101, 31))
            self.inoutf3.setObjectName ("inoutf3")
            self.inoutA3= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutA3.setGeometry (QtCore.QRect (310, 10, 101, 31))
            self.inoutA3.setObjectName ("inoutA3")
            self.label= QtWidgets.QLabel ('0', self.centralwidget)
            self.label.setGeometry (QtCore.QRect (10, 10, 16, 21))
            self.label.setObjectName ("label")
            self.label_2= QtWidgets.QLabel ('2', self.centralwidget)
            self.label_2.setGeometry (QtCore.QRect (150, 10, 16, 21))
            self.label_2.setObjectName ("label_2")
            self.label_3= QtWidgets.QLabel ('3', self.centralwidget)
            self.label_3.setGeometry (QtCore.QRect (290, 10, 16, 21))
            self.label_3.setObjectName ("label_3")
            self.label_4= QtWidgets.QLabel ('4', self.centralwidget)
            self.label_4.setGeometry (QtCore.QRect (10, 50, 16, 21))
            self.label_4.setObjectName ("label_4")
            self.label_5= QtWidgets.QLabel ('5', self.centralwidget)
            self.label_5.setGeometry (QtCore.QRect (5, 90, 21, 21))
            self.label_5.setObjectName ("label_5")
            self.label_6= QtWidgets.QLabel ('6', self.centralwidget)
            self.label_6.setGeometry (QtCore.QRect (150, 50, 16, 21))
            self.label_6.setObjectName ("label_6")self.label_7= QtWidgets.QLabel ('7', self.centralwidget) self.label_7.setGeometry (QtCore.QRect (145, 90, 21, 21))
            self.label_7.setObjectName ("label_7")
            self.label_8= QtWidgets.QLabel ('8', self.centralwidget)
            self.label_8.setGeometry (QtCore.QRect (285, 90, 21, 21))
            self.label_8.setObjectName ("label_8")
            self.label_9= QtWidgets.QLabel ('9', self.centralwidget)
            self.label_9.setGeometry (QtCore.QRect (290, 50, 16, 21))
            self.label_9.setObjectName ("label_9")
            self.inoutfd= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutfd.setGeometry (QtCore.QRect (30, 130, 101, 31))
            self.inoutfd.setObjectName ("inoutfd")
            self.label_10= QtWidgets.QLabel ('10 ', self.centralwidget)
            self.label_10.setGeometry (QtCore.QRect (10, 130, 21, 20))
            self.label_10.setObjectName ("label_10")
            self.inoutN= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutN.setGeometry (QtCore.QRect (170, 130, 104, 31))
            self.inoutN.setObjectName ("inoutN")
            self.label_11= QtWidgets.QLabel ('11 ', self.centralwidget)
            self.label_11.setGeometry (QtCore.QRect (150, 130, 16, 21))
            self.label_11.setObjectName ("label_11")
            self.btnplot= QtWidgets.QPushButton (self.centralwidget)
            self.btnplot.setGeometry (QtCore.QRect (310, 130, 101, 31))
            self.btnplot.setObjectName ("btnplot")
            self.widget= QtWidgets.QWidget (self.centralwidget)
            self.widget.setGeometry (QtCore.QRect (9, 169, 771, 371))
            self.widget.setObjectName ("widget")
            MainWindow.setCentralWidget (self.centralwidget)
            self.menubar= QtWidgets.QMenuBar (MainWindow)
            self.menubar.setGeometry (QtCore.QRect (0, 0, 785, 21))
            self.menubar.setObjectName ("menubar")
            MainWindow.setMenuBar (self.menubar)
            self.statusbar= QtWidgets.QStatusBar (MainWindow)
            self.statusbar.setObjectName ("statusbar")
            MainWindow.setStatusBar (self.statusbar)
            self.toolBar= QtWidgets.QToolBar (MainWindow)
            self.toolBar.setObjectName ("toolBar")
            MainWindow.addToolBar (QtCore.Qt.TopToolBarArea, self.toolBar)
    # self.retranslateUi (MainWindow)
    # QtCore.QMetaObject.connectSlotsByName (MainWindow)
    class MainWindow (QMainWindow, Ui_MainWindow):
        def __init __ (self):
            super () .__ init __ ()
            self.setupUi (self)
            self.btnplot.clicked.connect (self.plot_graph)
        def plot_graph (self):
            plt.close ()
            A1= int (self.inoutA1.toPlainText ())
            A2= int (self.inoutA2.toPlainText ())
            A3= int (self.inoutf3.toPlainText ())
            f1= int (self.inoutf1.toPlainText ())
            f2= int (self.inoutf2.toPlainText ())
            f3= int (self.inoutf3.toPlainText ())
            phi1= int (self.inoutphi1.toPlainText ())
            phi2= int (self.inoutphi2.toPlainText ())
            phi3= int (self.inoutphi3.toPlainText ())
            fd= int (self.inoutfd.toPlainText ())
            N= int (self.inoutN.toPlainText ())
            tstep= 1 /fd
            t= np.linspace (0, (N-1) * tstep, N)
            fstep= fd /N
            f= np.linspace (0, (N-1) * fstep, N)
            y= A1 * np.sin (2 * np.pi * f1 * tstep * t + phi1) + A2 * np.sin (2 * np.pi * f2 * tstep * t + phi2) + A3 * np.sin ( 2 * np.pi * f3 * tstep * t + phi3)
            X= np.fft.fft (y) # X -will be a series of complex numbers
            X_mag= np.abs (X) /N # Normalize X
            f_plot= f [0: int (N /2 + 1)]
            X_mag_plot= 2 * X_mag [0: int (N /2 + 1)]
            X_mag_plot [0]= X_mag_plot [0] /2
            # //////////////////////
            fig, ax= plt.subplots (1, 2, figsize= (4, 2))
            ax [0] .set (xlabel= 'time, s', ylabel= 'y', title= 'Signal')
            ax [1] .set (xlabel= 'freq, Hz', ylabel= 'y', title= 'Frequency spectrum')
            ax [0] .plot (t, y, '.-')
            ax [1] .plot (f_plot, X_mag_plot, '.-')
            ax [0] .grid ()
            ax [1] .grid ()
            fig.savefig ("test.png")
            plt.show ()
    if __name__== '__main__':
        import sys
        app= QApplication (sys.argv)
        ex= MainWindow ()
        ex.setWindowTitle (". docx ->
     .pdf ")
        ex.resize (540, 250)
        ex.show ()
        sys.exit (app.exec_ ())
    

    Thank you very much! Everything worked out)

    Игорь Туркин2021-09-27 23:34:00
  • Answer # 2

    Here is your implementation with plotting in a separate window.

    If you want to place your charts in the main application window, then see how it might look.

    Then, if you need to, try your own implementation.

    If something does not work out -ask a new question and please, your example should be minimally reproducible...

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QApplication, QMainWindow
    from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
    import matplotlib.pyplot as plt
    import numpy as np
    class Ui_MainWindow (object):
        def setupUi (self, MainWindow):
            MainWindow.setObjectName ("MainWindow")
            MainWindow.resize (785, 594)
            self.centralwidget= QtWidgets.QWidget (MainWindow)
            self.centralwidget.setObjectName ("centralwidget")
            self.inoutA1= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutA1.setGeometry (QtCore.QRect (30, 10, 101, 31))
            self.inoutA1.setObjectName ("inoutA1")
            self.inoutf1= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutf1.setGeometry (QtCore.QRect (30, 50, 101, 31))
            self.inoutf1.setObjectName ("inoutf1")
            self.inoutphi1= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutphi1.setGeometry (QtCore.QRect (30, 90, 101, 31))
            self.inoutphi1.setObjectName ("inoutphi1")
            self.plainTextEdit_4= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.plainTextEdit_4.setGeometry (QtCore.QRect (170, 90, 104, 31))
            self.plainTextEdit_4.setObjectName ("plainTextEdit_4")
            self.inoutphi2= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutphi2.setGeometry (QtCore.QRect (170, 90, 104, 31))
            self.inoutphi2.setObjectName ("inoutphi2")
            self.inoutf2= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutf2.setGeometry (QtCore.QRect (170, 50, 104, 31))
            self.inoutf2.setObjectName ("inoutf2")
            self.inoutA2= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutA2.setGeometry (QtCore.QRect (170, 10, 104, 31))
            self.inoutA2.setObjectName ("inoutA2")
            self.inoutphi3= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutphi3.setGeometry (QtCore.QRect (310, 90, 101, 31))
            self.inoutphi3.setObjectName ("inoutphi3")
            self.inoutf3= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutf3.setGeometry (QtCore.QRect (310, 50, 101, 31))
            self.inoutf3.setObjectName ("inoutf3")
            self.inoutA3= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutA3.setGeometry (QtCore.QRect (310, 10, 101, 31))
            self.inoutA3.setObjectName ("inoutA3")
            self.label= QtWidgets.QLabel ('0', self.centralwidget)
            self.label.setGeometry (QtCore.QRect (10, 10, 16, 21))
            self.label.setObjectName ("label")
            self.label_2= QtWidgets.QLabel ('2', self.centralwidget)
            self.label_2.setGeometry (QtCore.QRect (150, 10, 16, 21))
            self.label_2.setObjectName ("label_2")
            self.label_3= QtWidgets.QLabel ('3', self.centralwidget)
            self.label_3.setGeometry (QtCore.QRect (290, 10, 16, 21))
            self.label_3.setObjectName ("label_3")
            self.label_4= QtWidgets.QLabel ('4', self.centralwidget)
            self.label_4.setGeometry (QtCore.QRect (10, 50, 16, 21))
            self.label_4.setObjectName ("label_4")
            self.label_5= QtWidgets.QLabel ('5', self.centralwidget)
            self.label_5.setGeometry (QtCore.QRect (5, 90, 21, 21))
            self.label_5.setObjectName ("label_5")
            self.label_6= QtWidgets.QLabel ('6', self.centralwidget)
            self.label_6.setGeometry (QtCore.QRect (150, 50, 16, 21))
            self.label_6.setObjectName ("label_6")self.label_7= QtWidgets.QLabel ('7', self.centralwidget) self.label_7.setGeometry (QtCore.QRect (145, 90, 21, 21))
            self.label_7.setObjectName ("label_7")
            self.label_8= QtWidgets.QLabel ('8', self.centralwidget)
            self.label_8.setGeometry (QtCore.QRect (285, 90, 21, 21))
            self.label_8.setObjectName ("label_8")
            self.label_9= QtWidgets.QLabel ('9', self.centralwidget)
            self.label_9.setGeometry (QtCore.QRect (290, 50, 16, 21))
            self.label_9.setObjectName ("label_9")
            self.inoutfd= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutfd.setGeometry (QtCore.QRect (30, 130, 101, 31))
            self.inoutfd.setObjectName ("inoutfd")
            self.label_10= QtWidgets.QLabel ('10 ', self.centralwidget)
            self.label_10.setGeometry (QtCore.QRect (10, 130, 21, 20))
            self.label_10.setObjectName ("label_10")
            self.inoutN= QtWidgets.QPlainTextEdit (self.centralwidget)
            self.inoutN.setGeometry (QtCore.QRect (170, 130, 104, 31))
            self.inoutN.setObjectName ("inoutN")
            self.label_11= QtWidgets.QLabel ('11 ', self.centralwidget)
            self.label_11.setGeometry (QtCore.QRect (150, 130, 16, 21))
            self.label_11.setObjectName ("label_11")
            self.btnplot= QtWidgets.QPushButton (self.centralwidget)
            self.btnplot.setGeometry (QtCore.QRect (310, 130, 101, 31))
            self.btnplot.setObjectName ("btnplot")
            self.widget= QtWidgets.QWidget (self.centralwidget)
            self.widget.setGeometry (QtCore.QRect (9, 169, 771, 371))
            self.widget.setObjectName ("widget")
            MainWindow.setCentralWidget (self.centralwidget)
            self.menubar= QtWidgets.QMenuBar (MainWindow)
            self.menubar.setGeometry (QtCore.QRect (0, 0, 785, 21))
            self.menubar.setObjectName ("menubar")
            MainWindow.setMenuBar (self.menubar)
            self.statusbar= QtWidgets.QStatusBar (MainWindow)
            self.statusbar.setObjectName ("statusbar")
            MainWindow.setStatusBar (self.statusbar)
            self.toolBar= QtWidgets.QToolBar (MainWindow)
            self.toolBar.setObjectName ("toolBar")
            MainWindow.addToolBar (QtCore.Qt.TopToolBarArea, self.toolBar)
    # self.retranslateUi (MainWindow)
    # QtCore.QMetaObject.connectSlotsByName (MainWindow)
    class MainWindow (QMainWindow, Ui_MainWindow):
        def __init __ (self):
            super () .__ init __ ()
            self.setupUi (self)
            self.btnplot.clicked.connect (self.plot_graph)
        def plot_graph (self):
            plt.close ()
            A1= int (self.inoutA1.toPlainText ())
            A2= int (self.inoutA2.toPlainText ())
            A3= int (self.inoutf3.toPlainText ())
            f1= int (self.inoutf1.toPlainText ())
            f2= int (self.inoutf2.toPlainText ())
            f3= int (self.inoutf3.toPlainText ())
            phi1= int (self.inoutphi1.toPlainText ())
            phi2= int (self.inoutphi2.toPlainText ())
            phi3= int (self.inoutphi3.toPlainText ())
            fd= int (self.inoutfd.toPlainText ())
            N= int (self.inoutN.toPlainText ())
            tstep= 1 /fd
            t= np.linspace (0, (N-1) * tstep, N)
            fstep= fd /N
            f= np.linspace (0, (N-1) * fstep, N)
            y= A1 * np.sin (2 * np.pi * f1 * tstep * t + phi1) + A2 * np.sin (2 * np.pi * f2 * tstep * t + phi2) + A3 * np.sin ( 2 * np.pi * f3 * tstep * t + phi3)
            X= np.fft.fft (y) # X -will be a series of complex numbers
            X_mag= np.abs (X) /N # Normalize X
            f_plot= f [0: int (N /2 + 1)]
            X_mag_plot= 2 * X_mag [0: int (N /2 + 1)]
            X_mag_plot [0]= X_mag_plot [0] /2
            # //////////////////////
            fig, ax= plt.subplots (1, 2, figsize= (4, 2))
            ax [0] .set (xlabel= 'time, s', ylabel= 'y', title= 'Signal')
            ax [1] .set (xlabel= 'freq, Hz', ylabel= 'y', title= 'Frequency spectrum')
            ax [0] .plot (t, y, '.-')
            ax [1] .plot (f_plot, X_mag_plot, '.-')
            ax [0] .grid ()
            ax [1] .grid ()
            fig.savefig ("test.png")
            plt.show ()
    if __name__== '__main__':
        import sys
        app= QApplication (sys.argv)
        ex= MainWindow ()
        ex.setWindowTitle (". docx ->
     .pdf ")
        ex.resize (540, 250)
        ex.show ()
        sys.exit (app.exec_ ())
    

    Thank you very much! Everything worked out)

    Игорь Туркин2021-09-27 23:34:00