Как сделать чтобы окно при выдачи ответа gpt не расширялось сильно, а текст переносился вниз.
Ещё нужно исправит, чтобы при всех значениях 1 или 0 в расчетах приложение не закрывалось
import sys
import io
import os

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding=‘utf-8’)

from g4f.client import Client

client = Client()

from PyQt6.QtCore import pyqtSlot
from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox
import sys
import os
from Authorization import Ui_Authorization
from Registration import Ui_Registration
from MainWindow import Ui_MainWindow

DATA_FILE = “user_data.txt”

class AuthorizationWindow(QMainWindow, Ui_Authorization):
def init(self):
super(AuthorizationWindow, self).init()
self.setupUi(self)

# Handle “Зарегистрироваться” button click
self.pushButton_3.clicked.connect(self.open_registration)

# Handle “Войти” button click
self.pushButton.clicked.connect(self.login)

self.user_data = self.load_user_data()

def load_user_data(self):
“”“Load user data from a file.”“”
if not os.path.exists(DATA_FILE):
return {}

user_data = {}
with open(DATA_FILE, “r”) as file:
for line in file:
parts = line.strip().split(“,”)
if len(parts) == 2: # Старый формат, только имя пользователя и пароль
username, password = parts
email = “” # Email отсутствует в старом формате
elif len(parts) == 3: # Новый формат, включает email
username, password, email = parts
else:
continue # Неправильный формат строки, пропускаем

user_data[username] = {“password”: password, “email”: email}
return user_data

def save_user_data(self):
“”“Save user data to a file.”“”
with open(DATA_FILE, “w”) as file:
for username, data in self.user_data.items():
file.write(f"{username},{data[‘password’]},{data[‘email’]}\n")

@pyqtSlot()
def open_registration(self):
self.registration_window = RegistrationWindow(self)
self.registration_window.show()

@pyqtSlot()
def login(self):
username = self.lineEdit.text()
password = self.lineEdit_2.text()

msg_box = QMessageBox(self)
msg_box.setStyleSheet(“background-color: white;”)

if username in self.user_data and self.user_data[username][“password”] == password:
msg_box.setText(“Вы успешно вошли в систему!”)
msg_box.setIcon(QMessageBox.Icon.Information)

# Открываем главное окно и закрываем окно авторизации
self.main_window = MainWindowWindow(self)
self.main_window.show()
self.close()
else:
msg_box.setText(“Неверный логин или пароль!”)
msg_box.setIcon(QMessageBox.Icon.Warning)

msg_box.exec()

class RegistrationWindow(QMainWindow, Ui_Registration):
def init(self, auth_window):
super(RegistrationWindow, self).init()
self.setupUi(self)

# Reference to the authorization window to store user data
self.auth_window = auth_window

# Handle “Сохранить” button click
self.pushButton.clicked.connect(self.save_registration)

@pyqtSlot()
def save_registration(self):
email = self.lineEdit_3.text()
username = self.lineEdit.text()
password = self.lineEdit_2.text()
confirm_password = self.lineEdit_4.text()

msg_box = QMessageBox(self)
msg_box.setStyleSheet(“background-color: white;”)

if password != confirm_password:
msg_box.setText(“Пароли не совпадают!”)
msg_box.setIcon(QMessageBox.Icon.Warning)
msg_box.exec()
return

self.auth_window.user_data[username] = {“password”: password, “email”: email}
self.auth_window.save_user_data()
msg_box.setText(“Регистрация успешна!”)
msg_box.setIcon(QMessageBox.Icon.Information)
msg_box.exec()

self.close()

class MainWindowWindow(QMainWindow, Ui_MainWindow):
def init(self, auth_window):
super(MainWindowWindow, self).init()
self.setupUi(self)
self.auth_window = auth_window
self.initialize_signals()

# Initialize the first tab display
self.tabWidget.setCurrentIndex(0)

def initialize_signals(self):
# Connect comboBox signals to methods
self.comboBox_3.currentIndexChanged.connect(self.handle_comboBox_3_change)
self.comboBox_6.currentIndexChanged.connect(self.handle_comboBox_6_change)

# Connect pushButton signals to methods
self.pushButton_2.clicked.connect(self.handle_push_button_2)
self.pushButton_3.clicked.connect(self.handle_push_button_3)

self.pushButton_2.clicked.connect(self.switch_to_tab8)
self.pushButton_3.clicked.connect(self.switch_to_tab7)

self.pushButton_4.clicked.connect(self.handle_addition)

def handle_comboBox_3_change(self, index):
if index == 0: # Index for “1”
self.tabWidget.setCurrentIndex(0) # Set to tab3
elif index == 1: # Index for “2”
self.tabWidget.setCurrentIndex(1) # Set to tab4

def handle_comboBox_6_change(self, index):
if index == 0: # Index for “3”
self.tabWidget.setCurrentIndex(2) # Set to tab5

def handle_push_button_2(self):
# Set tabWidget to tab2
self.tabWidget_3.setCurrentIndex(1)

def handle_push_button_3(self):
# Set tabWidget to tab (initial tab)
self.tabWidget_3.setCurrentIndex(0)

def switch_to_tab8(self):
# Change the active tab in tabWidget3 to tab7
self.tabWidget_2.setCurrentIndex(1)

def switch_to_tab7(self):
# Change the active tab in tabWidget3 to tab6
self.tabWidget_2.setCurrentIndex(0)

#
# Анализ показателей финансовой устойчивости
def handle_addition(self):
# 1 год
try:
num1 = float(self.lineEdit_110.text())
num2 = float(self.lineEdit_126.text())
num3 = float(self.lineEdit_86.text())
result = (num1 + num2) / num3
self.label_156.setText(str(round(result, 3)))
except ValueError:
self.label_156.setText(“Ошибка ввода”)

try:
num3 = float(self.lineEdit_86.text())
num4 = float(self.lineEdit_29.text())
num5 = float(self.lineEdit_23.text())
result2 = (num3 - num4) / num5
self.label_172.setText(str(round(result2, 3)))
except ValueError:
self.label_172.setText(“Ошибка ввода”)

try:
num3 = float(self.lineEdit_86.text())
num6 = float(self.lineEdit_127.text())
result3 = num3 / num6
self.label_180.setText(str(round(result3, 3)))
except ValueError:
self.label_180.setText(“Ошибка ввода”)

try:
num3 = float(self.lineEdit_86.text())
num1 = float(self.lineEdit_110.text())
num2 = float(self.lineEdit_126.text())
result4 = num3 / (num1 + num2)
self.label_164.setText(str(round(result4, 3)))
except ValueError:
self.label_164.setText(“Ошибка ввода”)

try:
num3 = float(self.lineEdit_86.text())
num1 = float(self.lineEdit_110.text())
num6 = float(self.lineEdit_127.text())
result5 = (num3 + num1) / num6
self.label_148.setText(str(round(result5, 3)))
except ValueError:
self.label_148.setText(“Ошибка ввода”)

# 2 год
try:
num21 = float(self.lineEdit_116.text())
num22 = float(self.lineEdit_146.text())
num23 = float(self.lineEdit_93.text())
result21 = (num21 + num22) / num23
self.label_158.setText(str(round(result21, 3)))
except ValueError:
self.label_158.setText(“Ошибка ввода”)

try:
num23 = float(self.lineEdit_93.text())
num24 = float(self.lineEdit_47.text())
num25 = float(self.lineEdit_66.text())
result22 = (num23 - num24) / num25
self.label_173.setText(str(round(result22, 3)))
except ValueError:
self.label_173.setText(“Ошибка ввода”)

try:
num23 = float(self.lineEdit_93.text())
num26 = float(self.lineEdit_147.text())
result23 = num23 / num26
self.label_181.setText(str(round(result23, 3)))
except ValueError:
self.label_181.setText(“Ошибка ввода”)

try:
num23 = float(self.lineEdit_93.text())
num21 = float(self.lineEdit_116.text())
num22 = float(self.lineEdit_146.text())
result24 = num23 / (num21 + num22)
self.label_165.setText(str(round(result24, 3)))
except ValueError:
self.label_165.setText(“Ошибка ввода”)

try:
num23 = float(self.lineEdit_93.text())
num21 = float(self.lineEdit_116.text())
num26 = float(self.lineEdit_147.text())
result25 = (num23 + num21) / num26
self.label_149.setText(str(round(result25, 3)))
except ValueError:
self.label_149.setText(“Ошибка ввода”)

# 3 год
try:
num31 = float(self.lineEdit_117.text())
num32 = float(self.lineEdit_145.text())
num33 = float(self.lineEdit_100.text())
result31 = (num31 + num32) / num33
self.label_157.setText(str(round(result31, 3)))
except ValueError:
self.label_157.setText(“Ошибка ввода”)

try:
num33 = float(self.lineEdit_100.text())
num34 = float(self.lineEdit_48.text())
num35 = float(self.lineEdit_71.text())
result32 = (num33 - num34) / num35
self.label_174.setText(str(round(result32, 3)))
except ValueError:
self.label_174.setText(“Ошибка ввода”)

try:
num33 = float(self.lineEdit_100.text())
num36 = float(self.lineEdit_148.text())
result33 = num33 / num36
self.label_182.setText(str(round(result33, 3)))
except ValueError:
self.label_182.setText(“Ошибка ввода”)

try:
num33 = float(self.lineEdit_100.text())
num31 = float(self.lineEdit_117.text())
num32 = float(self.lineEdit_145.text())
result34 = num33 / (num31 + num32)
self.label_166.setText(str(round(result34, 3)))
except ValueError:
self.label_166.setText(“Ошибка ввода”)

try:
num33 = float(self.lineEdit_100.text())
num31 = float(self.lineEdit_117.text())
num36 = float(self.lineEdit_148.text())
result35 = (num33 + num31) / num36
self.label_150.setText(str(round(result35, 3)))
except ValueError:
self.label_150.setText(“Ошибка ввода”)

# Вывод в labels
try:
label_157 = float(self.label_157.text())
label_156 = float(self.label_156.text())

diff = label_157 - label_156
self.label_159.setText(str(round(diff, 3)))

ratio = label_157 / label_156
self.label_161.setText(str(round(ratio, 3)))

percent = (label_157 / label_156) * 100
self.label_160.setText(str(round(percent, 3)))

except ValueError:
self.label_159.setText(“Ошибка ввода”)
self.label_161.setText(“Ошибка ввода”)
self.label_160.setText(“Ошибка ввода”)

try:
label_174 = float(self.label_174.text())
label_172 = float(self.label_172.text())

diff_174_172 = label_174 - label_172
self.label_175.setText(str(round(diff_174_172, 3)))

ratio_174_172 = label_174 / label_172
self.label_176.setText(str(round(ratio_174_172, 3)))

percent_174_172 = (label_174 / label_172) * 100
self.label_177.setText(str(round(percent_174_172, 3)))

except ValueError:
self.label_175.setText(“Ошибка ввода”)
self.label_176.setText(“Ошибка ввода”)
self.label_177.setText(“Ошибка ввода”)

try:
label_182 = float(self.label_182.text())
label_180 = float(self.label_180.text())

diff_182_180 = label_182 - label_180
self.label_183.setText(str(round(diff_182_180, 3)))

ratio_182_180 = label_182 / label_180
self.label_184.setText(str(round(ratio_182_180, 3)))

percent_182_180 = (label_182 / label_180) * 100
self.label_185.setText(str(round(percent_182_180, 3)))

except ValueError:
self.label_183.setText(“Ошибка ввода”)
self.label_184.setText(“Ошибка ввода”)
self.label_185.setText(“Ошибка ввода”)

try:
label_166 = float(self.label_166.text())
label_164 = float(self.label_164.text())

diff_166_164 = label_166 - label_164
self.label_167.setText(str(round(diff_166_164, 3)))

ratio_166_164 = label_166 / label_164
self.label_168.setText(str(round(ratio_166_164, 3)))

percent_166_164 = (label_166 / label_164) * 100
self.label_169.setText(str(round(percent_166_164, 3)))

except ValueError:
self.label_167.setText(“Ошибка ввода”)
self.label_168.setText(“Ошибка ввода”)
self.label_169.setText(“Ошибка ввода”)

try:
label_150 = float(self.label_150.text())
label_148 = float(self.label_148.text())

diff_150_148 = label_150 - label_148
self.label_151.setText(str(round(diff_150_148, 3)))

ratio_150_148 = label_150 / label_148
self.label_152.setText(str(round(ratio_150_148, 3)))

percent_150_148 = (label_150 / label_148) * 100
self.label_153.setText(str(round(percent_150_148, 3)))

except ValueError:
self.label_151.setText(“Ошибка ввода”)
self.label_152.setText(“Ошибка ввода”)
self.label_153.setText(“Ошибка ввода”)

# Отправка данных в GPT и установка ответа в label_139
self.send_to_gpt()

def send_to_gpt(self):
# Формируем сообщение для GPT
u1_values = (self.label_156.text(), self.label_158.text(), self.label_157.text())
u11_values = (self.label_159.text(), self.label_161.text(), self.label_160.text())
u2_values = (self.label_172.text(), self.label_173.text(), self.label_174.text())
u21_values = (self.label_175.text(), self.label_176.text(), self.label_177.text())
u3_values = (self.label_180.text(), self.label_181.text(), self.label_182.text())
u31_values = (self.label_183.text(), self.label_184.text(), self.label_185.text())
u4_values = (self.label_164.text(), self.label_165.text(), self.label_166.text())
u41_values = (self.label_167.text(), self.label_168.text(), self.label_169.text())
u5_values = (self.label_148.text(), self.label_149.text(), self.label_150.text())
u51_values = (self.label_151.text(), self.label_152.text(), self.label_153.text())

content = f"““Значения коэффициентов:
1. Коэффициент финансового риска, U1: за 1 год, 2 год, 3 год,{u1_values}, измерительные показатели Абсолютное откланение, Относительно откланение, Темп роста {u11_values}
2. Коэффициент обеспеченности собственными источниками финансирования, U2: за 1 год, 2 год, 3 год, {u2_values}, измерительные показатели Абсолютное откланение, Относительно откланение, Темп роста {u21_values}
3. Коэффициент финансовой независимости (автономии), U3: за 1 год, 2 год, 3 год, {u3_values}, измерительные показатели Абсолютное откланение, Относительно откланение, Темп роста {u31_values}
4. Коэффициент финансирования, U4: за 1 год, 2 год, 3 год, {u4_values}, измерительные показатели Абсолютное откланение, Относительно откланение, Темп роста {u41_values}
5. Коэффициент финансовой устойчивости, U5: за 1 год, 2 год, 3 год, {u5_values}, измерительные показатели Абсолютное откланение, Относительно откланение, Темп роста {u51_values}

Пожалуйста, проанализируйте эти коэффициенты и дайте рекомендации по улучшению финансовой устойчивости компании. А также какие изменения показали измерительные показатели””"

response = client.chat.completions.create(
model=“gpt-3.5-turbo”,
messages=[{“role”: “user”, “content”: content}],
)

# Выводим ответ ChatGPT в label_139
self.label_139.setText(response.choices[0].message.content)

#
def main():
app = QApplication(sys.argv)

auth_window = AuthorizationWindow()
auth_window.show()

sys.exit(app.exec())

if name == “main”:
main()