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
#1
Hello forum members. I hope everyone is doing well.

Can someone please look at the code below and tell me what i am doing wrong. I've been using the same messagebox in other projects as well without any issues but whenever i use the messagebox here the Python interpreter crashes without giving out any error messages.

Any help would be appreciated.

from PyQt5 import QtWidgets, QtCore
import mysql.connector
import sys
from fuzzywuzzy import fuzz, process

.
.
.
.

def connectDB():
    try:
        connection = mysql.connector.connect(host='######', user='######', password='######')
        cursor = connection.cursor()
        cursor.execute('USE client_list;')
    except:
        msg = QtWidgets.QMessageBox(3, "Warning", "Admin action required", QtWidgets.QMessageBox.Ok)
        msg.exec()


if __name__ == '__main__':
    connectDB()
    app = QtWidgets.QApplication(sys.argv)
    MainApplication = MainWindow()
    app.exec()
I've left out the mainwindow initialization and configuration as i don't think its related to the issue, however, if anyone deems it necessary i can also show the complete code.

Regards
iMu
Reply
#2
did you not show the MainWindow() ?

if __name__ == '__main__':
    connectDB()
    app = QtWidgets.QApplication(sys.argv)
    MainApplication = MainWindow()
    MainApplication.show()  ## show main window
    app.exec()
Reply
#3
(Aug-23-2019, 07:22 AM)Axel_Erfurt Wrote: did you not show the MainWindow() ?

if __name__ == '__main__':
    connectDB()
    app = QtWidgets.QApplication(sys.argv)
    MainApplication = MainWindow()
    MainApplication.show()  ## show main window
    app.exec()

Yeah i've set it to show through my setupUI method, the complete code is below ...

from PyQt5 import QtWidgets
import mysql.connector
import sys
from fuzzywuzzy import fuzz, process

class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.client_name = QtWidgets.QLineEdit()
        self.check_button = QtWidgets.QPushButton("Add Client to BABAR AKRAM")
        self.check_button.clicked.connect(self.addName)

        self.setupUI()

    def setupUI(self):
        container = QtWidgets.QGridLayout()
        container.addWidget(QtWidgets.QLabel("Client Name"), 0, 0)
        container.addWidget(self.client_name, 0, 1)
        container.addWidget(self.check_button, 1, 0, 1, 2)
        self.setWindowTitle("For Updating Client List Manually")
        self.setLayout(container)
        self.show()

    def addName(self):
        print("Add button clicked")


def connectDB():
    try:
        connection = mysql.connector.connect(host='######', user='######', password='######')
        cursor = connection.cursor()
        cursor.execute('USE client_list;')
    except:
        msg = QtWidgets.QMessageBox(3, "Warning", "Please check the internet connection", QtWidgets.QMessageBox.Ok)
        msg.exec()


if __name__ == '__main__':
    connectDB()
    app = QtWidgets.QApplication(sys.argv)
    MainApplication = MainWindow()
    app.exec()
Reply
#4
Not sure that is your problem, but you should use "exec_" instead of "exec", which is a reserved python method since version 3.
Reply
#5
(Aug-23-2019, 11:41 AM)Alfalfa Wrote: Not sure that is your problem, but you should use "exec_" instead of "exec", which is a reserved python method since version 3.

I've tried that too. On a side note I thought exec_ was only left because of PyQt4 compatibility since exec is no longer a reserved word in Python 3.
Reply
#6
Exec is part of Python 3 built-functions. The message box appears in blank "except" condition. This is bad practice as it will catch everything and may hide other errors. Try narrowing the problem down by removing some part of your code. For instance, does it still crash like this?

def connectDB():
    msg = QtWidgets.QMessageBox(3, "Warning", "Please check the internet connection", QtWidgets.QMessageBox.Ok)
    msg.exec_()
Prehaps the dialog is collected by the garbage collector? Try to bind "msg" to "self" so the object persist after the loop.
Reply
#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
#8
I figured out the problem tho, i was trying to initialize the Message Box without starting the QT application main loop first i.e. QtWidget.QApplication(sys.argv)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GUI crashes flash77 3 865 Jul-26-2023, 05:09 PM
Last Post: flash77
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,199 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  [PyQt] App crashes when reopening a subwindow JayCee 13 4,933 Aug-04-2021, 01:51 AM
Last Post: JayCee
  [PyGUI] My GUI crashes after command MLGpotato 1 1,857 Feb-21-2021, 03:17 PM
Last Post: deanhystad
  [PyQt] PyQT5 Thread crashes after a while Suresh 0 1,951 Jul-21-2020, 07:53 AM
Last Post: Suresh
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,785 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