Calibration constant kt: Remove all exec and eval.

dev
Pierre-Antoine 2025-05-05 14:39:42 +02:00
parent a6e81b3329
commit bf8640fb30
1 changed files with 60 additions and 41 deletions

View File

@ -21,18 +21,17 @@
import os import os
import pandas as pd import pandas as pd
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QDialog, QTabWidget, QGridLayout, QScrollArea, from PyQt5.QtWidgets import (
QFileDialog, QMessageBox, QLabel) QWidget, QVBoxLayout, QDialog, QTabWidget, QGridLayout,
QScrollArea, QFileDialog, QMessageBox, QLabel
)
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
class CalibrationConstantKt(QDialog): class CalibrationConstantKt(QDialog):
def __init__(self, parent=None): def __init__(self, parent=None):
super(CalibrationConstantKt, self).__init__(parent) super(CalibrationConstantKt, self).__init__(parent)
self.setGeometry(400, 200, 300, 400) self.setGeometry(400, 200, 300, 400)
self.setWindowTitle("Calibration constant kt") self.setWindowTitle("Calibration constant kt")
self.verticalLayout_Main = QVBoxLayout() self.verticalLayout_Main = QVBoxLayout()
@ -41,15 +40,18 @@ class CalibrationConstantKt(QDialog):
self.verticalLayout_Main.addWidget(self.tab) self.verticalLayout_Main.addWidget(self.tab)
try: 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: except FileNotFoundError as e:
msgBox = QMessageBox() msgBox = QMessageBox()
msgBox.setWindowTitle("File Not Found Error") msgBox.setWindowTitle("File Not Found Error")
msgBox.setIcon(QMessageBox.Warning) msgBox.setIcon(QMessageBox.Warning)
msgBox.setText("Please check Excel file name for the calibration constant kt \n" msgBox.setText(
"It should be an excel file named : 'ABS_calibration_constant_kt.xlsx'") "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.setStandardButtons(QMessageBox.Ok)
msgBox.exec() msgBox.exec()
@ -57,9 +59,11 @@ class CalibrationConstantKt(QDialog):
self.load_freq_and_kt_values() self.load_freq_and_kt_values()
def open_dialog_box(self): def open_dialog_box(self):
filename = QFileDialog.getOpenFileNames(self, "Calibration file", "", "Calibration file (*.xlsx)", filename = QFileDialog.getOpenFileNames(
options=QFileDialog.DontUseNativeDialog) self, "Calibration file", "", "Calibration file (*.xlsx)",
print(filename) options=QFileDialog.DontUseNativeDialog
)
dir_name = os.path.dirname(filename[0][0]) dir_name = os.path.dirname(filename[0][0])
file_name = os.path.basename(filename[0][0]) file_name = os.path.basename(filename[0][0])
print(f"dir name : {dir_name} & file name : {file_name}") print(f"dir name : {dir_name} & file name : {file_name}")
@ -71,43 +75,58 @@ class CalibrationConstantKt(QDialog):
self.load_freq_and_kt_values() self.load_freq_and_kt_values()
def load_freq_and_kt_values(self): def load_freq_and_kt_values(self):
for t in range(len(self.data_ABS.keys())): for t in range(len(self.data_ABS.keys())):
self.tab.removeTab(t) self.tab.removeTab(t)
for t_index, t_value in enumerate(list(self.data_ABS.keys())): 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()") verticalLayout = QVBoxLayout(tab_calib)
eval("self.tab.addTab(self.tab_calib_" + str(t_index) + ", '" + str(t_value) + "')")
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) + ")") verticalLayout.addWidget(scrollarea)
eval("self.scrollarea_tab_" + str(t_index) + ".setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)") gridLayout = QGridLayout(scrollarea)
eval("self.scrollarea_tab_" + str(t_index) + ".setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)")
eval("self.scrollarea_tab_" + str(t_index) + ".setWidgetResizable(True)")
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(): 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)')") label_freq = QLabel(
exec("self.label_kt_" + str(t_index) + " = QLabel('" + str([*self.data_ABS.values()][t_index].columns[1]) + " (V.m<sup>1.5</sup>)')") f"{[*self.data_ABS.values()][t_index].columns[0]} (MHz)"
)
label_kt = QLabel(
f"{[*self.data_ABS.values()][t_index].columns[1]} "
+ "(V.m<sup>1.5</sup>)"
)
else: else:
exec("self.label_freq_" + str(t_index) + " = QLabel('Frequency (MHz)')") label_freq = QLabel('Frequency (MHz)')
exec("self.label_kt_" + str(t_index) + " = QLabel('kt (V.m<sup>1.5</sup>)')") label_kt = QLabel('kt (V.m<sup>1.5</sup>)')
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)") 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]): for x in range(self.data_ABS[t_value].shape[0]):
exec("self.label_freq_" + str(x) + "_ABS_" + str(t_index) + " = QLabel()") label_freq_ABS = 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)')") label_freq_ABS.setText(
# eval("self.label_freq_" + str(x) + "_ABS_" + str(t_index) + ".setDisabled(True)") f"{self.data_ABS[t_value].iloc[x][0]*1e-6}" + "(MHz)"
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)") gridLayout.addWidget(
exec("self.label_kt_" + str(x) + "_ABS_" + str(t_index) + " = QLabel()") label_freq_ABS, x + 1, 0, 1, 1,Qt.AlignCenter
exec("self.label_kt_" + str(x) + "_ABS_" + str(t_index) + ".setText('" + str(self.data_ABS[t_value].iloc[x][1]) + "V.m<sup>1.5</sup>')") )
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) + label_kt_ABS = QLabel()
", " + str(x+1) + ", 1, 1, 1, Qt.AlignCenter)")
label_kt_ABS.setText(
f"{self.data_ABS[t_value].iloc[x][1]} V.m<sup>1.5</sup>"
)
label_kt_ABS.setStyleSheet('border: 1px solid black;')
gridLayout.addWidget(
label_kt_ABS, x + 1 , 1, 1, 1, Qt.AlignCenter
)