Python Forum
PyQt5 : Interpreter Crashes While Initializing Message Box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5 : Interpreter Crashes While Initializing Message Box
#7
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
iMuny likes this post
Reply


Messages In This Thread
RE: PyQt5 : Interpreter Crashes While Initializing Message Box - by Denni - Aug-23-2019, 07:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  GUI crashes flash77 3 970 Jul-26-2023, 05:09 PM
Last Post: flash77
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,283 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  [PyQt] App crashes when reopening a subwindow JayCee 13 5,177 Aug-04-2021, 01:51 AM
Last Post: JayCee
  [PyGUI] My GUI crashes after command MLGpotato 1 1,925 Feb-21-2021, 03:17 PM
Last Post: deanhystad
  [PyQt] PyQT5 Thread crashes after a while Suresh 0 2,012 Jul-21-2020, 07:53 AM
Last Post: Suresh
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,863 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020