Создал графический интерфейс в QtDesigner и у меня есть условный файл design.ui. Также есть основной код который подсчитывает матрицу методом Крамера или Гауса. Искал в интернете разные гайды не нашел толкового объяснения как код и интерфейс засунуть в .exe вместе.
Код программы:
from PyQt6 import uic, QtWidgets
from PyQt6.QtGui import QFont
import numpy as np
Form, _ = uic.loadUiType("2.ui")
def gauss(self, A, b):
n = len(b)
for i in range(n):
if A[i][i] == 0:
return None
for j in range(i+1, n):
k = A[j][i] / A[i][i]
for l in range(i, n):
A[j][l] -= k * A[i][l]
b[j] -= k * b[i]
x = [0] * n
for i in range(n-1, -1, -1):
s = 0
for j in range(i+1, n):
s += A[i][j] * x[j]
x[i] = (b[i] - s) / A[i][i]
return x
class Ui(QtWidgets.QDialog, Form):
def init(self, parent=None):
super(Ui, self).init(parent)
self.setupUi(self)
self.start.clicked.connect(self.printButtonPressed)
print('da')
def printButtonPressed(self):
if self.Kramer.isChecked():
x1 = self.x1.text()
x2 = self.x2.text()
x3 = self.x3.text()
x4 = self.x4.text()
y1 = self.y1.text()
y2 = self.y2.text()
y3 = self.y3.text()
y4 = self.y4.text()
z1 = self.z1.text()
z2 = self.z2.text()
z3 = self.z3.text()
z4 = self.z4.text()
h1 = self.h1.text()
h2 = self.h2.text()
h3 = self.h3.text()
h4 = self.h4.text()
o1 = self.o1.text()
o2 = self.o2.text()
o3 = self.o3.text()
o4 = self.o4.text()
if x1.replace("-", "").isdigit() and x2.replace("-", "").isdigit() and x3.replace("-", "").isdigit() and x4.replace("-", "").isdigit() and y1.replace("-", "").isdigit() and y2.replace("-", "").isdigit() and y3.replace("-", "").isdigit() and y4.replace("-", "").isdigit() and z1.replace("-", "").isdigit() and z2.replace("-", "").isdigit() and z3.replace("-", "").isdigit() and z4.replace("-", "").isdigit() and h1.replace("-", "").isdigit() and h2.replace("-", "").isdigit() and h3.replace("-", "").isdigit() and h4.replace("-", "").isdigit() and o1.replace("-", "").isdigit() and o2.replace("-", "").isdigit() and o3.replace("-", "").isdigit() and o4.replace("-", "").isdigit():
x1 = float(x1)
x2 = float(x2)
x3 = float(x3)
x4 = float(x4)
y1 = float(y1)
y2 = float(y2)
y3 = float(y3)
y4 = float(y4)
z1 = float(z1)
z2 = float(z2)
z3 = float(z3)
z4 = float(z4)
h1 = float(h1)
h2 = float(h2)
h3 = float(h3)
h4 = float(h4)
o1 = float(o1)
o2 = float(o2)
o3 = float(o3)
o4 = float(o4)
A = np.array([[x1, x2, x3, x4],
[y1, y2, y3, y4],
[z1, z2, z3, z4],
[h1, h2, h3, h4]])
b = np.array([o1, o2, o3, o4])
det_A = np.linalg.det(A)
if det_A == 0:
self.otvet.setText("0")
else:
x = np.zeros(len(A))
for i in range(len(A)):
Ai = A.copy()
Ai[:, i] = b
det_Ai = np.linalg.det(Ai)
if det_Ai == 0:
print("0")
else:
x[i] = det_Ai / det_A
font2 = QFont()
font2.setPointSize(48)
self.otvet.setFont(font2)
print(str(x[i]))
t = [0,0,0,0]
for n in range (4):
j = int(x[n])
t[n] = float(j)
t[n] = round(x[n], 4)
t1 = str(t[0])
t2 = str(t[1])
t3 = str(t[2])
t4 = str(t[3])
otv = (t1[:4] + '|' + t2[:4] + '|' + t3[:4] + '|' + t4[:4])
self.otvet.setText(otv)
else:
font = QFont()
font.setPointSize(12)
self.otvet.setFont(font)
self.otvet.setText("Ошибка: значения должны быть целыми числами")
elif self.Gause.isChecked():
print('гаус принят')
x1 = self.x1.text()
x2 = self.x2.text()
x3 = self.x3.text()
x4 = self.x4.text()
y1 = self.y1.text()
y2 = self.y2.text()
y3 = self.y3.text()
y4 = self.y4.text()
z1 = self.z1.text()
z2 = self.z2.text()
z3 = self.z3.text()
z4 = self.z4.text()
h1 = self.h1.text()
h2 = self.h2.text()
h3 = self.h3.text()
h4 = self.h4.text()
o1 = self.o1.text()
o2 = self.o2.text()
o3 = self.o3.text()
o4 = self.o4.text()
print('числа принял')
if x1.replace("-", "").isdigit() and x2.replace("-", "").isdigit() and x3.replace("-", "").isdigit() and x4.replace("-", "").isdigit() and y1.replace("-", "").isdigit() and y2.replace("-", "").isdigit() and y3.replace("-", "").isdigit() and y4.replace("-", "").isdigit() and z1.replace("-", "").isdigit() and z2.replace("-", "").isdigit() and z3.replace("-", "").isdigit() and z4.replace("-", "").isdigit() and h1.replace("-", "").isdigit() and h2.replace("-", "").isdigit() and h3.replace("-", "").isdigit() and h4.replace("-", "").isdigit() and o1.replace("-", "").isdigit() and o2.replace("-", "").isdigit() and o3.replace("-", "").isdigit() and o4.replace("-", "").isdigit():
x1 = float(x1)
x2 = float(x2)
x3 = float(x3)
x4 = float(x4)
y1 = float(y1)
y2 = float(y2)
y3 = float(y3)
y4 = float(y4)
z1 = float(z1)
z2 = float(z2)
z3 = float(z3)
z4 = float(z4)
h1 = float(h1)
h2 = float(h2)
h3 = float(h3)
h4 = float(h4)
o1 = float(o1)
o2 = float(o2)
o3 = float(o3)
o4 = float(o4)
print('минуса принял')
A = np.array([[x1, x2, x3, x4],
[y1, y2, y3, y4],
[z1, z2, z3, z4],
[h1, h2, h3, h4]])
b = np.array([o1, o2, o3, o4])
print(A,b)
x = gauss(A, b)
print("x прошли")
if x == None:
font = QFont()
font.setPointSize(12)
self.otvet.setFont(font)
self.otvet.setText("Ошибка: не может быть решено по методу Гауса")
else:
print('Ответ начался')
t = [0] * 4
for n in range (4):
print(x[n])
t[n] = str(round(x[n], 4))
otv = (t[0][:4] + '|' + t[1][:4] + '|' + t[2][:4] + '|' + t[3][:4])
self.otvet.setText(otv)
else:
font = QFont()
font.setPointSize(12)
self.otvet.setFont(font)
self.otvet.setText("Ошибка: значения должны быть целыми числами")
else:
u = str(-1)
u = float(u)
print(u)
font1 = QFont()
font1.setPointSize(18)
self.otvet.setFont(font1)
self.otvet.setText("Вы не выбрали метод")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Ui()
w.show()
sys.exit(app.exec())