I've created a program in python that controls an instrument (a power source) using the pyvisa library. The program has a GUI I designed with qtcreator where the file format is a .ui which then I convert to a .py using pyuic6: basically I did the same of this video except I used pyuic6 instead of 5.
Everything worked and I kept adding functionalities till I decided, for security reasons, to catch the pressure of the "x" button that closes the program so I can tell to my power supply to switch-off and avoid dangerous situations.
I found examples to do that with the closeEvent method, like the answer of this question. The example works as is, but when I try to put it on my code seems the closeEvent method it isn't triggered when I actually close the windows.
here some code:
The file I launch "main.py"
import datetime
import math
import os
import queue
import subprocess
import sys
import threading
import time
import exitwindow
import infoloadwindow
from tkinter import filedialog
import matplotlib.pyplot as plt
import pandas as pd
import pyvisa as pv
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
from PyQt6.QtCore import QObject, QThread, pyqtSignal
from qtpy import QtWidgets
from form_v32 import Ui_TESTALIM
--- some code ---
class MiaUi(Ui_TESTALIM):
c_voltage, c_current, c_resistance, c_power = 0.0, 0.0, 0.0, 0.0
def __init__(self, Widget):
self.setupUi(Widget)
self.tableWidget.verticalHeader().setVisible(False)
self.horizontalLayout_20 = QtWidgets.QHBoxLayout(
self.frame_grafico)
self.horizontalLayout_20.setObjectName("horizontalLayout_20")
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.horizontalLayout_20.addWidget(
self.canvas)
self.check()
def check(self):
self.start_on.clicked.connect(self.lettura_cicli)
self.aggiungi.clicked.connect(self.aggiungi_riga)
self.carica_da_file.clicked.connect(self.load_from_csv)
self.elimina.clicked.connect(self.cancella_riga)
self.ovp_on.clicked.connect(self.ovp_acceso)
self.ovp_off.clicked.connect(self.ovp_spento)
self.ocp_on.clicked.connect(self.ocp_acceso)
self.ocp_off.clicked.connect(self.ocp_spento)
self.start_off.clicked.connect(self.spegni)
self.salvataggi.clicked.connect(self.apricartella)
self.collega_strumento.clicked.connect(self.collegamento)
def closeEvent(self, event):
print("I guess it worked")
--- other methods ---
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MyWidget = QtWidgets.QWidget()
ui = MiaUi(MyWidget)
MyWidget.show()
MyWidget.setFixedSize(1110, 600)
sys.exit(app.exec())
The gui file "form_v32.py"
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_TESTALIM(object):
def setupUi(self, TESTALIM):
TESTALIM.setObjectName("TESTALIM")
TESTALIM.resize(1110, 600)
TESTALIM.setAutoFillBackground(False)
TESTALIM.setStyleSheet("background-color: rgb(255,255,255);\n"
"border-color: rgb(0, 0, 0);")
--- other code ---
def retranslateUi(self, TESTALIM):
_translate = QtCore.QCoreApplication.translate
TESTALIM.setWindowTitle(_translate("TESTALIM", "Widget"))
self.savings.setText(_translate("TESTALIM", "save"))
-- other code and eof---
My feeling is that closeEvent isn't seen as a method of form_V32 but only as a method of the class MiaUI, and that's why it does not trigger anything, but I can't understand the cause. I have the doubt something changed related to this from Pyqt5 to Pyqt6, but I'm not sure. I have even the doubt I might miss some sort of import so I reported all of them.
Thanks in advance