Python Forum

Full Version: PyQT5 : Unable to Create Another Dialog While One is Open
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear All

Hope you all are doing well. I've just recently ventured into the world of PyQt through online tuts etc. and it's feeling a bit overwhelming due to the huge resources available. I've started making a small application but am facing an issue where when i try to create a dialog box while one dialog box is already opened it doesn't open. The area where I'm facing the issue is commented in the code.


# -*- coding: utf-8 -*-

from PyQt5 import QtWidgets
import mysql.connector
import sys


class MainApplication(QtWidgets.QWidget):

    def __init__(self):
        super(MainApplication, self).__init__()

        self.setWindowTitle("Main Window")
        loginstatus = self.get_login()

        if loginstatus:
            self.show()

    def get_login(self):
        login = LoginDlg()
        if not login.exec_():
            if login.OKID:
                print("ID Found")
                return True


class MsgBox(QtWidgets.QDialog):

    def __init__(self, message, msgtitle):
        super(MsgBox, self).__init__()
        self.message = message
        self.msgtitle = msgtitle
        self.setupUI()

    def setupUI(self):
        messagebox = QtWidgets.QHBoxLayout()
        messagebox.addStretch(1)
        usermessage = QtWidgets.QLabel(self.message)
        messagebox.addWidget(usermessage)
        self.setWindowTitle(self.msgtitle)
        self.setLayout(messagebox)
        self.show()
        

class LoginDlg(QtWidgets.QDialog):

    def __init__(self):
        super(LoginDlg, self).__init__()
        self.OKID = False
        self.username = QtWidgets.QLineEdit()
        self.password = QtWidgets.QLineEdit()
        self.setupUI(self.username, self.password)

    def setupUI(self, username, password):
        entry_box = QtWidgets.QFormLayout()
        entry_box.addRow("Username", username)
        entry_box.addRow("Password", password)

        button_box = QtWidgets.QHBoxLayout()
        button_box.addStretch(1)
        ok_button = QtWidgets.QPushButton("OK")
        cancel_button = QtWidgets.QPushButton("Cancel")
        button_box.addWidget(ok_button)
        button_box.addWidget(cancel_button)

        main_box = QtWidgets.QVBoxLayout()
        main_box.stretch(1)
        main_box.addLayout(entry_box)
        main_box.addLayout(button_box)

        self.setWindowTitle("Login")
        self.setLayout(main_box)

        ok_button.clicked.connect(self.ok_pressed)
        cancel_button.clicked.connect(self.cancel_pressed)


    def ok_pressed(self):
        connection = mysql.connector.connect(host="192.168.0.168", user="LISTAPP", password="samba786")
        cursor = connection.cursor()
        cursor.execute("USE client_list;")
        username = self.username.text()
        password = self.password.text()
        cursor.execute("SELECT * from auth;")
        userData = [(each[1], each[2]) for each in cursor.fetchall()]
        if (username, password) in userData:
            self.close()
            self.OKID = True
        else:

            # This window below does not open
            errormsg = MsgBox("User ID not found", "Invalid data")
            # This window above does not open

            print("ID not found")

    def cancel_pressed(self):
        self.close()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainApplication = MainApplication()

    sys.exit(app.exec_())
Any help in the above regards would be highly appreciated.

Regards
iMu
your dialog does never end.

here is an example for an input Dialog

from PyQt5 import QtWidgets

class Login(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Login, self).__init__(parent)
        self.textName = QtWidgets.QLineEdit(self)
        self.textPass = QtWidgets.QLineEdit(self)
        self.textPass.setEchoMode(QtWidgets.QLineEdit.Password)
        self.buttonLogin = QtWidgets.QPushButton('Login', self)
        self.buttonLogin.clicked.connect(self.handleLogin)

        self.buttonCancel = QtWidgets.QPushButton('Cancel', self)
        self.buttonCancel.clicked.connect(self.handleCancel)

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.textName)
        layout.addWidget(self.textPass)
        layout.addWidget(self.buttonLogin)
        layout.addWidget(self.buttonCancel)

    def handleCancel(self):
        self.close()

    def handleLogin(self):
        if (self.textName.text() == 'foo' and
            self.textPass.text() == 'bar'):
            self.accept()
        else:
            QtWidgets.QMessageBox.warning(
                self, 'Error', 'Bad user or password')

class Window(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

    def msgbox(self, message):
        msg = QtWidgets.QMessageBox(1, "Information", message, QtWidgets.QMessageBox.Ok)
        msg.setStyleSheet("QLabel{min-width: 100px;}")
        msg.exec()

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    login = Login()

    if login.exec_() == QtWidgets.QDialog.Accepted:
        window = Window()
        window.show()
        window.msgbox("it works!")
        sys.exit(app.exec_())
The second dialog is destroyed by python's garbage collector. Simply add your dialog as an attribute of self:

self.errormsg = MsgBox("User ID not found", "Invalid data")
But if all you want is a message box, you should rather use QMessageBox instead:

msg = QtWidgets.QMessageBox()
msg.setWindowFlags(msg.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
msg.setIcon(QtWidgets.QMessageBox.Warning)
msg.setWindowTitle("Your title")
msg.setText("Your message")
msg.setStandardButtons(QtWidgets.QMessageBox.Apply | QtWidgets.QMessageBox.Cancel)
msg.setDefaultButton(QtWidgets.QMessageBox.Cancel)
if msg.exec_() == QtWidgets.QMessageBox.Apply:
    print("apply")
Thank you to the both of you. That helped me work it out. Solved.

Regards
iMu