Unsolved Pyqt5- QMessageBox is appearing blank & program getting crashed
-
I am trying to run a code to load dataframe into a sql server using PyQT5 and also at the same time showing a "Please wait" pop up while data is being loaded in DB. I am utilizing QThread for the same. If there is some error then QMessageBox shows the error to the user. But my program is crashing with Message Box appearing blank. I am attaching both code and error snippet.Error Snippet
from functools import partial from PyQt5 import QtCore, QtGui, QtWidgets import configparser import pyodbc def ConfigSection(section,Config): dict1 = {} options = Config.options(section) for option in options: try: dict1[option] = Config.get(section,option) except: dict1[option] = None return dict1 class DatabaseWorker(QtCore.QObject): started = QtCore.pyqtSignal() finished = QtCore.pyqtSignal() message = QtCore.pyqtSignal(object) msg = "" @QtCore.pyqtSlot() def writeToDatabase(self, final_pivot): self.started.emit() Config = configparser.ConfigParser(interpolation=None) try: config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.ini') Config.read_string(open(config_file).read()) config_values = ConfigSection("Global", Config) driver = config_values.get('driver') server = config_values.get('server') database = config_values.get('database') username = config_values.get('username') password = config_values.get('password') con_str = ("Driver={" + driver + "};" "Server=" + server + ";" "Database=" + database + ";" "UID=" + username + ";" "PWD=" + password + ";") cnxn = pyodbc.connect(con_str) cursor = cnxn.cursor() for i, row in final_pivot.iterrows(): sql = "INSERT INTO dbo.[FPP_Cleansed_MarketData] (ID,Region,Geography,Category,Company,Year,Adjusted_Sales,Sales_Units,Unit_Type) VALUES (" + "?," * ( len(row) - 1) + "?)" cursor.execute(sql, tuple(row)) cnxn.commit() except pyodbc.Error as ex: sqlstate = ex.args[0] if sqlstate == '28000': msg = "Error: Error while logging in. Please check login credentials" self.message.emit(msg) credentials") return -1 elif sqlstate == '23000': msg = "Error: Trying to insert duplicate ID" self.message.emit(msg) return -1 else: msg = "Error: Error encountered while inserting records into Data Warehouse" self.message.emit(msg) return -1 finally: self.finished.emit() class Windows_GUI(QtWidgets.QMainWindow): def __init__(self, df): QtWidgets.QMainWindow.__init__(self) self.__threads = [] self.thread = QtCore.QThread(parent=self) self.__threads.append(self.thread) self.thread.start() self.m_database_worker = DatabaseWorker() self.m_database_worker.moveToThread(self.thread) self.m_database_worker.started.connect(self.start_animation) self.m_database_worker.message.connect(self.show_message) wrapper = partial(self.m_database_worker.writeToDatabase,df) QtCore.QTimer.singleShot(0, wrapper) @QtCore.pyqtSlot() def start_animation(self): gif_path = "loading.gif" self.loading_window = QtWidgets.QDialog() self.loading_window.setWindowFlags(QtCore.Qt.SplashScreen) self.loading_window.setAttribute(QtCore.Qt.WA_DeleteOnClose) movie = QtGui.QMovie(gif_path, cacheMode=QtGui.QMovie.CacheAll) movie_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter) movie_label.setFixedSize(100, 75) movie_label.setMovie(movie) text_label = QtWidgets.QLabel('Data is getting loaded into Data Warehouse. Please wait...') text_label.setFont(QtGui.QFont('Arial',11)) vbox = QtWidgets.QHBoxLayout(self.loading_window) vbox.addWidget(movie_label) vbox.addWidget(text_label) self.m_database_worker.finished.connect(self.close_window) movie.start() self.setVisible(False) self.loading_window.show() def show_message(self,msg): try: self.msg_box = QtWidgets.QMessageBox() self.msg_box.setWindowTitle('Error') self.msg_box.setIcon(QtWidgets.QMessageBox.Critical) self.msg_box.information(self, 'Message', msg) #self.msg_box.setText("Hello Rock the Brahma Bull") self.msg_box.move(self.frameGeometry().center()) self.msg_box.exec_() except Exception as e: print(e) def close_window(self): for thread in self.__threads: thread.quit() thread.wait() self.close() if __name__ == "__main__": import sys import pandas as pd app = QtWidgets.QApplication(sys.argv) df = pd.read_excel('test.xlsx') df['ID'] = ['Project_id_' + str(i) for i in range(len(df))] df = df[['ID', 'Regions', 'Geographies', 'Categories', 'Companies', 'Year', 'Sales', 'Sales_Unit','Currency Conversion']] w = Windows_GUI(df) w.show() sys.exit(app.exec_())
-
Hi and welcome to devnet,
What is the back trace of your crash ?