Okay I do not have your mysql module but so I just created an error instead :) and the code works just fine with the tweaks I made to it so here it is I hope this helps
#import mysql.connector from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel from PyQt5.QtWidgets import QLineEdit, QPushButton, QMessageBox from sys import exit as sysExit class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle("For Updating Client List Manually") self.lneClientName = QLineEdit() self.btnAdder = QPushButton("Add Client to BABAR AKRAM") self.btnAdder.clicked.connect(self.AddName) container = QGridLayout() container.addWidget(QLabel('Client Name'), 0, 0) container.addWidget(self.lneClientName, 0, 1) container.addWidget(self.btnAdder, 1, 0, 1, 2) self.setLayout(container) def AddName(self): print("Add button clicked") self.DBase = connectDB() def connectDB(): try: divisor = 0 test = 4 / divisor # connection = mysql.connector.connect(host='######', user='######', password='######') # cursor = connection.cursor() # cursor.execute('USE client_list;') except: msg = QMessageBox(3, "Warning", "Please check the internet connection", QMessageBox.Ok) msg.exec_() if __name__ == '__main__': MainThred = QApplication([]) MainGui = MainWindow() MainGui.show() sysExit(MainThred.exec())Note I moved your database connection into your MainWindow as you are not really going to use it outside of that context and if you are then you need to have your __main__ call whatever application controller you plan to use that would then in turn call the MainWindow as it stands only the most exterior to the program things should take place in __main__ such as the 4 things shown and perhaps getting command line arguments and the like. Also I only put it inside the button click to get the window and then allow the testing of the error message window. Ideally it would be part of the MainWindow Init -- and it would be its own self-contained encapsulated Class