From bf8640fb306fe02a02359a077759b962f797d733 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Mon, 5 May 2025 14:39:42 +0200 Subject: [PATCH] Calibration constant kt: Remove all exec and eval. --- Model/calibration_constant_kt.py | 101 ++++++++++++++++++------------- 1 file changed, 60 insertions(+), 41 deletions(-) diff --git a/Model/calibration_constant_kt.py b/Model/calibration_constant_kt.py index 4f3c00c..02844bb 100644 --- a/Model/calibration_constant_kt.py +++ b/Model/calibration_constant_kt.py @@ -21,18 +21,17 @@ import os import pandas as pd -from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QDialog, QTabWidget, QGridLayout, QScrollArea, - QFileDialog, QMessageBox, QLabel) +from PyQt5.QtWidgets import ( + QWidget, QVBoxLayout, QDialog, QTabWidget, QGridLayout, + QScrollArea, QFileDialog, QMessageBox, QLabel +) from PyQt5.QtCore import Qt class CalibrationConstantKt(QDialog): - def __init__(self, parent=None): - super(CalibrationConstantKt, self).__init__(parent) self.setGeometry(400, 200, 300, 400) - self.setWindowTitle("Calibration constant kt") self.verticalLayout_Main = QVBoxLayout() @@ -41,15 +40,18 @@ class CalibrationConstantKt(QDialog): self.verticalLayout_Main.addWidget(self.tab) try: - self.data_ABS = pd.read_excel("ABS_calibration_constant_kt.xlsx", header=0, sheet_name=None) - + self.data_ABS = pd.read_excel( + "ABS_calibration_constant_kt.xlsx", + header=0, sheet_name=None + ) except FileNotFoundError as e: - msgBox = QMessageBox() msgBox.setWindowTitle("File Not Found Error") msgBox.setIcon(QMessageBox.Warning) - msgBox.setText("Please check Excel file name for the calibration constant kt \n" - "It should be an excel file named : 'ABS_calibration_constant_kt.xlsx'") + msgBox.setText( + "Please check Excel file name for the calibration constant kt \n" + "It should be an excel file named : 'ABS_calibration_constant_kt.xlsx'" + ) msgBox.setStandardButtons(QMessageBox.Ok) msgBox.exec() @@ -57,9 +59,11 @@ class CalibrationConstantKt(QDialog): self.load_freq_and_kt_values() def open_dialog_box(self): - filename = QFileDialog.getOpenFileNames(self, "Calibration file", "", "Calibration file (*.xlsx)", - options=QFileDialog.DontUseNativeDialog) - print(filename) + filename = QFileDialog.getOpenFileNames( + self, "Calibration file", "", "Calibration file (*.xlsx)", + options=QFileDialog.DontUseNativeDialog + ) + dir_name = os.path.dirname(filename[0][0]) file_name = os.path.basename(filename[0][0]) print(f"dir name : {dir_name} & file name : {file_name}") @@ -71,43 +75,58 @@ class CalibrationConstantKt(QDialog): self.load_freq_and_kt_values() def load_freq_and_kt_values(self): - for t in range(len(self.data_ABS.keys())): self.tab.removeTab(t) for t_index, t_value in enumerate(list(self.data_ABS.keys())): + tab_calib = QWidget() + self.tab.addTab(tab_calib, t_value) - exec("self.tab_calib_" + str(t_index) + "= QWidget()") - eval("self.tab.addTab(self.tab_calib_" + str(t_index) + ", '" + str(t_value) + "')") + verticalLayout = QVBoxLayout(tab_calib) - exec("self.verticalLayout_tab_" + str(t_index) + "= QVBoxLayout(self.tab_calib_" + str(t_index) + ")") + scrollarea = QScrollArea(tab_calib) + scrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) + scrollarea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + scrollarea.setWidgetResizable(True) - exec("self.scrollarea_tab_" + str(t_index) + " = QScrollArea(self.tab_calib_" + str(t_index) + ")") - eval("self.scrollarea_tab_" + str(t_index) + ".setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)") - eval("self.scrollarea_tab_" + str(t_index) + ".setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)") - eval("self.scrollarea_tab_" + str(t_index) + ".setWidgetResizable(True)") + verticalLayout.addWidget(scrollarea) + gridLayout = QGridLayout(scrollarea) - eval("self.verticalLayout_tab_" + str(t_index) + ".addWidget(self.scrollarea_tab_" + str(t_index) + ")") - - exec("self.gridLayout_tab_" + str(t_index) + " = QGridLayout(self.scrollarea_tab_" + str(t_index) + ")") if [*self.data_ABS.values()][t_index].columns.any(): - exec("self.label_freq_" + str(t_index) + " = QLabel('" + str([*self.data_ABS.values()][t_index].columns[0]) + " (MHz)')") - exec("self.label_kt_" + str(t_index) + " = QLabel('" + str([*self.data_ABS.values()][t_index].columns[1]) + " (V.m1.5)')") + label_freq = QLabel( + f"{[*self.data_ABS.values()][t_index].columns[0]} (MHz)" + ) + label_kt = QLabel( + f"{[*self.data_ABS.values()][t_index].columns[1]} " + + "(V.m1.5)" + ) else: - exec("self.label_freq_" + str(t_index) + " = QLabel('Frequency (MHz)')") - exec("self.label_kt_" + str(t_index) + " = QLabel('kt (V.m1.5)')") - eval("self.gridLayout_tab_" + str(t_index) + ".addWidget(self.label_freq_" + str(t_index) + ", 0, 0, 1, 1, Qt.AlignCenter)") - eval("self.gridLayout_tab_" + str(t_index) + ".addWidget(self.label_kt_" + str(t_index) + ", 0, 1, 1, 1, Qt.AlignCenter)") + label_freq = QLabel('Frequency (MHz)') + label_kt = QLabel('kt (V.m1.5)') + + gridLayout.addWidget( + label_freq, 0, 0, 1, 1, Qt.AlignCenter + ) + gridLayout.addWidget( + label_kt, 0, 1, 1, 1, Qt.AlignCenter + ) for x in range(self.data_ABS[t_value].shape[0]): - exec("self.label_freq_" + str(x) + "_ABS_" + str(t_index) + " = QLabel()") - # print(eval("self.label_freq_" + str(x) + "_ABS_" + str(t_index) + ".setText('" + str(self.data_ABS[t_value].iloc[x][0]*1e-6) + " MHz')")) - eval("self.label_freq_" + str(x) + "_ABS_" + str(t_index) + ".setText('" + str(self.data_ABS[t_value].iloc[x][0]*1e-6) + " (MHz)')") - # eval("self.label_freq_" + str(x) + "_ABS_" + str(t_index) + ".setDisabled(True)") - eval("self.gridLayout_tab_" + str(t_index) + ".addWidget(self.label_freq_" + str(x) + "_ABS_" + str(t_index) + - ", " + str(x + 1) + ", 0, 1, 1,Qt.AlignCenter)") - exec("self.label_kt_" + str(x) + "_ABS_" + str(t_index) + " = QLabel()") - exec("self.label_kt_" + str(x) + "_ABS_" + str(t_index) + ".setText('" + str(self.data_ABS[t_value].iloc[x][1]) + "V.m1.5')") - eval("self.label_kt_" + str(x) + "_ABS_" + str(t_index) + ".setStyleSheet('border: 1px solid black;')") - eval("self.gridLayout_tab_" + str(t_index) + ".addWidget(self.label_kt_" + str(x) + "_ABS_" + str(t_index) + - ", " + str(x+1) + ", 1, 1, 1, Qt.AlignCenter)") + label_freq_ABS = QLabel() + + label_freq_ABS.setText( + f"{self.data_ABS[t_value].iloc[x][0]*1e-6}" + "(MHz)" + ) + gridLayout.addWidget( + label_freq_ABS, x + 1, 0, 1, 1,Qt.AlignCenter + ) + + label_kt_ABS = QLabel() + + label_kt_ABS.setText( + f"{self.data_ABS[t_value].iloc[x][1]} V.m1.5" + ) + label_kt_ABS.setStyleSheet('border: 1px solid black;') + gridLayout.addWidget( + label_kt_ABS, x + 1 , 1, 1, 1, Qt.AlignCenter + )