Как выводить результат в главное окно?
Сделал выбор "Объект с самовыравниванием" и по нажатию кнопки у меня происходит вывод в терминал результата моей функции (w * kp
).
Как сделать вывод в это окно, к примеру под кнопкой "Расчет коэффициентов", какой виджет создавать?
from PyQt5 import QtCore , QtGui , QtWidgets , uic
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas ,
NavigationToolbar2QT as NavigationToolbar)
import matplotlib.pyplot as plt
from PyQt5.Qt import *
from PyQt5.QtWidgets import QLabel , QLineEdit
from scipy import integrate
from control.matlab import *
import control
import numpy as np
from scipy import interpolate
import scipy.optimize as opt
class Ui_MainWindow ( object ) :
def setupUi(self , MainWindow) :
MainWindow.setObjectName ( "MainWindow" )
MainWindow.resize ( 1400 , 900 )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Ignored , QtWidgets.QSizePolicy.Ignored )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( MainWindow.sizePolicy ().hasHeightForWidth () )
MainWindow.setSizePolicy ( sizePolicy )
MainWindow.setMinimumSize ( QtCore.QSize ( 1400 , 900 ) )
MainWindow.setToolButtonStyle ( QtCore.Qt.ToolButtonIconOnly )
self.centralwidget = QtWidgets.QWidget ( MainWindow )
self.centralwidget.setObjectName ( "centralwidget" )
self.gridLayout = QtWidgets.QGridLayout ( self.centralwidget )
self.gridLayout.setContentsMargins ( 0 , 0 , 0 , 0 )
self.gridLayout.setHorizontalSpacing ( 7 )
self.gridLayout.setVerticalSpacing ( 0 )
self.gridLayout.setObjectName ( "gridLayout" )
self.label_3 = QtWidgets.QLabel ( self.centralwidget )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Minimum , QtWidgets.QSizePolicy.Preferred )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.label_3.sizePolicy ().hasHeightForWidth () )
self.label_3.setSizePolicy ( sizePolicy )
self.label_3.setMinimumSize ( QtCore.QSize ( 0 , 35 ) )
self.label_3.setMaximumSize ( QtCore.QSize ( 16777215 , 35 ) )
font = QtGui.QFont ()
font.setPointSize ( 12 )
font.setBold ( True )
font.setWeight ( 75 )
font.setKerning ( False )
self.label_3.setFont ( font )
self.label_3.setStyleSheet ( "background-color: rgb(255, 255, 255);" )
self.label_3.setFrameShape ( QtWidgets.QFrame.StyledPanel )
self.label_3.setFrameShadow ( QtWidgets.QFrame.Raised )
self.label_3.setAlignment ( QtCore.Qt.AlignCenter )
self.label_3.setObjectName ( "label_3" )
self.gridLayout.addWidget ( self.label_3 , 1 , 0 , 1 , 1 )
self.horizontalLayout_3 = QtWidgets.QHBoxLayout ()
self.horizontalLayout_3.setObjectName ( "horizontalLayout_3" )
self.groupBox_3 = QtWidgets.QGroupBox ( self.centralwidget )
font = QtGui.QFont ()
font.setPointSize ( 10 )
font.setBold ( True )
font.setWeight ( 75 )
self.groupBox_3.setFont ( font )
self.groupBox_3.setObjectName ( "groupBox_3" )
self.verticalLayout_4 = QtWidgets.QVBoxLayout ( self.groupBox_3 )
self.verticalLayout_4.setObjectName ( "verticalLayout_4" )
self.mplwindow = QtWidgets.QWidget ( self.groupBox_3 )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Fixed , QtWidgets.QSizePolicy.Fixed )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.mplwindow.sizePolicy ().hasHeightForWidth () )
self.mplwindow.setSizePolicy ( sizePolicy )
self.mplwindow.setLayoutDirection ( QtCore.Qt.LeftToRight )
self.mplwindow.setObjectName ( "mplwindow" )
self.mplvl = QtWidgets.QVBoxLayout ( self.mplwindow )
self.mplvl.setObjectName ( "mplvl" )
self.verticalLayout_4.addWidget ( self.mplwindow )
self.horizontalLayout_3.addWidget ( self.groupBox_3 )
self.mplfigs = QtWidgets.QListWidget ( self.centralwidget )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Minimum , QtWidgets.QSizePolicy.Minimum )
sizePolicy.setHorizontalStretch ( 100 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.mplfigs.sizePolicy ().hasHeightForWidth () )
self.mplfigs.setSizePolicy ( sizePolicy )
self.mplfigs.setMinimumSize ( QtCore.QSize ( 0 , 0 ) )
self.mplfigs.setMaximumSize ( QtCore.QSize ( 200 , 400 ) )
self.mplfigs.setObjectName ( "mplfigs" )
self.horizontalLayout_3.addWidget ( self.mplfigs )
self.gridLayout.addLayout ( self.horizontalLayout_3 , 4 , 0 , 1 , 1 )
self.label_4 = QtWidgets.QLabel ( self.centralwidget )
font = QtGui.QFont ()
font.setPointSize ( 12 )
font.setBold ( True )
font.setWeight ( 75 )
font.setKerning ( False )
self.label_4.setFont ( font )
self.label_4.setStyleSheet ( "background-color: rgb(255, 255, 255);" )
self.label_4.setFrameShape ( QtWidgets.QFrame.StyledPanel )
self.label_4.setFrameShadow ( QtWidgets.QFrame.Raised )
self.label_4.setAlignment ( QtCore.Qt.AlignCenter )
self.label_4.setObjectName ( "label_4" )
self.gridLayout.addWidget ( self.label_4 , 1 , 1 , 1 , 1 )
self.horizontalLayout_4 = QtWidgets.QHBoxLayout ()
self.horizontalLayout_4.setContentsMargins ( -1 , 5 , -1 , 0 )
self.horizontalLayout_4.setObjectName ( "horizontalLayout_4" )
self.groupBox = QtWidgets.QGroupBox ( self.centralwidget )
font = QtGui.QFont ()
font.setPointSize ( 10 )
font.setBold ( True )
font.setUnderline ( False )
font.setWeight ( 75 )
self.groupBox.setFont ( font )
self.groupBox.setInputMethodHints ( QtCore.Qt.ImhNone )
self.groupBox.setAlignment ( QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop )
self.groupBox.setFlat ( False )
self.groupBox.setCheckable ( False )
self.groupBox.setChecked ( False )
self.groupBox.setObjectName ( "groupBox" )
# self.groupBox.setStyleSheet ( "background-color: #e6c4c0;" )
self.formLayoutWidget = QtWidgets.QWidget ( self.groupBox )
self.formLayoutWidget.setGeometry ( QtCore.QRect ( 10 , 50 , 311 , 151 ) )
self.formLayoutWidget.setObjectName ( "formLayoutWidget" )
self.formLayout_2 = QtWidgets.QFormLayout ( self.formLayoutWidget )
self.formLayout_2.setContentsMargins ( 0 , 0 , 0 , 0 )
self.formLayout_2.setHorizontalSpacing ( 6 )
self.formLayout_2.setVerticalSpacing ( 12 )
self.formLayout_2.setObjectName ( "formLayout_2" )
self.Label = QtWidgets.QLabel ( self.formLayoutWidget )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Expanding , QtWidgets.QSizePolicy.Expanding )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.Label.sizePolicy ().hasHeightForWidth () )
self.Label.setSizePolicy ( sizePolicy )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.Label.setFont ( font )
self.Label.setObjectName ( "Label" )
self.formLayout_2.setWidget ( 0 , QtWidgets.QFormLayout.LabelRole , self.Label )
self.DoubleSpinBox = QtWidgets.QDoubleSpinBox ( self.formLayoutWidget )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Expanding , QtWidgets.QSizePolicy.Fixed )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.DoubleSpinBox.sizePolicy ().hasHeightForWidth () )
self.DoubleSpinBox.setSizePolicy ( sizePolicy )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.DoubleSpinBox.setFont ( font )
self.DoubleSpinBox.setObjectName ( "DoubleSpinBox" )
self.formLayout_2.setWidget ( 0 , QtWidgets.QFormLayout.FieldRole , self.DoubleSpinBox )
self.Label_2 = QtWidgets.QLabel ( self.formLayoutWidget )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.Label_2.setFont ( font )
self.Label_2.setObjectName ( "Label_2" )
self.formLayout_2.setWidget ( 1 , QtWidgets.QFormLayout.LabelRole , self.Label_2 )
self.DoubleSpinBox_2 = QtWidgets.QDoubleSpinBox ( self.formLayoutWidget )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Expanding , QtWidgets.QSizePolicy.Fixed )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.DoubleSpinBox_2.sizePolicy ().hasHeightForWidth () )
self.DoubleSpinBox_2.setSizePolicy ( sizePolicy )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.DoubleSpinBox_2.setFont ( font )
self.DoubleSpinBox_2.setObjectName ( "DoubleSpinBox_2" )
self.formLayout_2.setWidget ( 1 , QtWidgets.QFormLayout.FieldRole , self.DoubleSpinBox_2 )
self.Label_3 = QtWidgets.QLabel ( self.formLayoutWidget )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.Label_3.setFont ( font )
self.Label_3.setObjectName ( "Label_3" )
self.formLayout_2.setWidget ( 2 , QtWidgets.QFormLayout.LabelRole , self.Label_3 )
self.DoubleSpinBox_3 = QtWidgets.QDoubleSpinBox ( self.formLayoutWidget )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Expanding , QtWidgets.QSizePolicy.Fixed )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.DoubleSpinBox_3.sizePolicy ().hasHeightForWidth () )
self.DoubleSpinBox_3.setSizePolicy ( sizePolicy )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.DoubleSpinBox_3.setFont ( font )
self.DoubleSpinBox_3.setMinimum ( 0.0 )
self.DoubleSpinBox_3.setObjectName ( "DoubleSpinBox_3" )
self.formLayout_2.setWidget ( 2 , QtWidgets.QFormLayout.FieldRole , self.DoubleSpinBox_3 )
self.Label_4 = QtWidgets.QLabel ( self.formLayoutWidget )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.Label_4.setFont ( font )
self.Label_4.setObjectName ( "Label_4" )
self.formLayout_2.setWidget ( 3 , QtWidgets.QFormLayout.LabelRole , self.Label_4 )
self.SpinBox = QtWidgets.QSpinBox ( self.formLayoutWidget )
sizePolicy = QtWidgets.QSizePolicy ( QtWidgets.QSizePolicy.Expanding , QtWidgets.QSizePolicy.Fixed )
sizePolicy.setHorizontalStretch ( 0 )
sizePolicy.setVerticalStretch ( 0 )
sizePolicy.setHeightForWidth ( self.SpinBox.sizePolicy ().hasHeightForWidth () )
self.SpinBox.setSizePolicy ( sizePolicy )
font = QtGui.QFont ()
font.setBold ( False )
font.setWeight ( 50 )
self.SpinBox.setFont ( font )
self.SpinBox.setObjectName ( "SpinBox" )
self.formLayout_2.setWidget ( 3 , QtWidgets.QFormLayout.FieldRole , self.SpinBox )
self.gridLayoutWidget = QtWidgets.QWidget ( self.groupBox )
self.gridLayoutWidget.setGeometry ( QtCore.QRect ( 10 , 210 , 311 , 201 ) )
self.gridLayoutWidget.setObjectName ( "gridLayoutWidget" )
self.gridLayout_2 = QtWidgets.QGridLayout ( self.gridLayoutWidget )
self.gridLayout_2.setContentsMargins ( 0 , 0 , 0 , 0 )
self.gridLayout_2.setVerticalSpacing ( 10 )
self.gridLayout_2.setObjectName ( "gridLayout_2" )
self.label = QtWidgets.QLabel ( self.gridLayoutWidget )
self.label.setAlignment ( QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop )
self.label.setObjectName ( "label" )
self.gridLayout_2.addWidget ( self.label , 0 , 0 , 1 , 1 )
self.label_2 = QtWidgets.QLabel ( self.gridLayoutWidget )
self.label_2.setAlignment ( QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop )
self.label_2.setObjectName ( "label_2" )
self.gridLayout_2.addWidget ( self.label_2 , 0 , 1 , 1 , 1 )
scroll = QScrollArea ( self.groupBox )
scroll.setGeometry ( QtCore.QRect ( 10 , 215 , 311 , 200 ) )
scroll.setWidgetResizable ( True ) # +++
scroll.setWidget ( self.gridLayoutWidget )
self.horizontalLayout_4.addWidget ( self.groupBox )
self.groupBox_2 = QtWidgets.QGroupBox ( self.centralwidget )
font = QtGui.QFont ()
font.setPointSize ( 10 )
font.setBold ( True )
font.setWeight ( 75 )
self.groupBox_2.setFont ( font )
self.groupBox_2.setFlat ( False )
self.groupBox_2.setObjectName ( "groupBox_2" )
self.layoutWidget = QtWidgets.QWidget ( self.groupBox_2 )
self.layoutWidget.setGeometry ( QtCore.QRect ( 20 , 50 , 273 , 153 ) )
self.layoutWidget.setObjectName ( "layoutWidget" )
self.verticalLayout_2 = QtWidgets.QVBoxLayout ( self.layoutWidget )
self.verticalLayout_2.setContentsMargins ( 0 , 0 , 0 , 0 )
self.verticalLayout_2.setSpacing ( 16 )
self.verticalLayout_2.setObjectName ( "verticalLayout_2" )
self.rB1 = QtWidgets.QRadioButton ( self.layoutWidget )
font = QtGui.QFont ()
font.setBold ( False )
font.setUnderline ( True )
font.setWeight ( 50 )
self.rB1.setFont ( font )
self.rB1.setObjectName ( "rB1" )
self.verticalLayout_2.addWidget ( self.rB1 )
self.rB2 = QtWidgets.QRadioButton ( self.layoutWidget )
font = QtGui.QFont ()
font.setBold ( False )
font.setUnderline ( True )
font.setWeight ( 50 )
self.rB2.setFont ( font )
self.rB2.setObjectName ( "rB2" )
self.verticalLayout_2.addWidget ( self.rB2 )
self.rB3 = QtWidgets.QRadioButton ( self.layoutWidget )
font = QtGui.QFont ()
font.setBold ( False )
font.setUnderline ( True )
font.setWeight ( 50 )
self.rB3.setFont ( font )
self.rB3.setObjectName ( "rB3" )
self.verticalLayout_2.addWidget ( self.rB3 )
self.pushButton_2 = QtWidgets.QPushButton ( self.layoutWidget )
self.pushButton_2.setEnabled ( True )
font = QtGui.QFont ()
font.setPointSize ( 9 )
font.setBold ( False )
font.setUnderline ( True )
font.setWeight ( 50 )
font.setKerning ( False )
self.pushButton_2.setFont ( font )
self.pushButton_2.setObjectName ( "pushButton_2" )
self.verticalLayout_2.addWidget ( self.pushButton_2 )
self.horizontalLayout_4.addWidget ( self.groupBox_2 )
self.gridLayout.addLayout ( self.horizontalLayout_4 , 2 , 0 , 1 , 1 )
MainWindow.setCentralWidget ( self.centralwidget )
self.menubar = QtWidgets.QMenuBar ( MainWindow )
self.menubar.setGeometry ( QtCore.QRect ( 0 , 0 , 1400 , 26 ) )
self.menubar.setObjectName ( "menubar" )
MainWindow.setMenuBar ( self.menubar )
self.retranslateUi ( MainWindow )
QtCore.QMetaObject.connectSlotsByName ( MainWindow )
def retranslateUi(self , MainWindow) :
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle ( _translate ( "MainWindow" , "MainWindow" ) )
self.label_3.setText ( _translate ( "MainWindow" , "Идентификация объекта" ) )
self.groupBox_3.setTitle ( _translate ( "MainWindow" , "Кривая разгона" ) )
self.label_4.setText ( _translate ( "MainWindow" , "Расчет настроек регулятора" ) )
self.groupBox.setTitle ( _translate ( "MainWindow" , "Ввод кривой разгона" ) )
self.Label.setText ( _translate ( "MainWindow" , "Входное воздействие:" ) )
self.Label_2.setText ( _translate ( "MainWindow" , "Величина запаздывания:" ) )
self.Label_3.setText ( _translate ( "MainWindow" , "Шаг" ) )
self.Label_4.setText ( _translate ( "MainWindow" , "Количество точек:" ) )
self.label.setText ( _translate ( "MainWindow" , "Время" ) )
self.label_2.setText ( _translate ( "MainWindow" , "Выход" ) )
self.groupBox_2.setTitle ( _translate ( "MainWindow" , "Тип разгоной характеристики" ) )
self.rB1.setText ( _translate ( "MainWindow" , "Объект с самовыравниванием" ) )
self.rB2.setText ( _translate ( "MainWindow" , "Объект без самовыравнивания" ) )
self.rB3.setText ( _translate ( "MainWindow" , "Колебательный" ) )
self.pushButton_2.setText ( _translate ( "MainWindow" , "Расчет коэффициентов" ) )
class MainWindow ( QtWidgets.QMainWindow , Ui_MainWindow ) :
def __init__(self) :
super ().__init__ ()
self.setupUi ( self )
self.fig_dict = {}
self.mplfigs.itemClicked.connect ( self.changefig )
fig = Figure ()
self.addmpl ( fig )
self.pushButton_2.clicked.connect ( self.samovirav )
def samovirav(self) :
x_points = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
10 , 11 , 12 , 13 , 14 , 15 , 16]
y_points = [1 , 1 , 1.125 , 1.3 , 1.45 , 1.55 , 1.65 , 1.725 , 1.8 ,
1.85 , 1.89 , 1.91 , 1.95 , 1.96 , 1.975 , 1.985 , 2]
def h(i) :
self.tck = interpolate.splrep ( x_points , y_points )
return interpolate.splev ( i , self.tck )
def Rosenbrock(a) :
f = tf ( 1 , [a[0] , 1] );
g = tf ( 1 , [a[1] , 1] );
d = tf ( 1 , [a[2] , 1] );
w = f * g * d
# %% Generating transfer function of Pade approx :
T_delay = 1
n_pade = 5
(num_pade , den_pade) = control.pade ( T_delay , n_pade )
H_pade = control.tf ( num_pade , den_pade )
W = control.series ( H_pade , w )
T = np.arange ( 0 , 17 , 1 )
y , x = step ( (W+y_points[0]) , T)
d = (h ( T ) - y) ** 2;
y_int = integrate.simps ( d , T , axis=-1 , even='avg' )
return y_int
n = 3
# Вектор с двумя элементами типа float
a0 = np.zeros ( n , dtype=float )
# Начальная точка поиска минимума функции
a0[0] = 1
a0[1] = 1
a0[2] = 1
xtol = 1.0e-9 # Точность поиска экстремума
# Находим минимум функции
res = opt.minimize (
Rosenbrock ,
a0 ,
method='Nelder-Mead' ,
options={'xtol' : xtol , 'disp' : True}
)
c = (res.x)
f = tf ( 1 , [c[0] , 1] );
g = tf ( 1 , [c[1] , 1] );
d = tf ( 1 , [c[2] , 1] );
w = f * g * d
# %% Generating transfer function of Pade approx :
T_delay = 1
n_pade = 5
(num_pade , den_pade) = control.pade ( T_delay , n_pade )
H_pade = control.tf ( num_pade , den_pade )
W = control.series ( H_pade , w )
T = np.arange ( 0 , 17 , 1 )
y , x = step ( (W+y_points[0]) , T)
kp=6.2
print(c[0]*c[1]*c[2])
print ( c[0] * c[1] + c[0] * c[2] + c[1] * c[2] )
print ( c[0] + c[1] + c[2] )
print(w*kp)
print((res.x),(res.fun) )
# plt.plot ( x_points , h( x_points ) , 'o-r' , x , y , "b" )
# plt.grid ( True )
# plt.show ()
main.addfig ( 'Заданная КР', fig1 )
main.addfig ( f'Аппроксимированная {round(res.fun,3)}' , fig2 )
ax1f1.plot ( x_points , y_points , 'or' )
ax1f2.plot ( x_points , h ( x_points ) , 'o-r' , x , y , "b" )
def changefig(self, item):
text = item.text()
self.rmmpl()
self.addmpl(self.fig_dict[text])
def addfig(self , name , fig) :
self.fig_dict[name] = fig
self.mplfigs.addItem ( name )
def addmpl(self , fig) :
self.canvas = FigureCanvas ( fig )
self.mplvl.addWidget ( self.canvas )
self.canvas.draw ()
self.toolbar = NavigationToolbar ( self.canvas ,
self.mplwindow , coordinates=True )
self.mplvl.addWidget ( self.toolbar )
def rmmpl(self , ) :
self.mplvl.removeWidget ( self.canvas )
self.canvas.close ()
self.mplvl.removeWidget ( self.toolbar )
self.toolbar.close ()
if __name__ == "__main__":
import sys
fig1 = Figure()
ax1f1 = fig1.add_subplot(111)
ax1f1.grid ( True )
fig2 = Figure()
ax1f2 = fig2.add_subplot(111)
ax1f2.grid ( True )
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())