Python Forum
PyQT5 : Unable to Create Another Dialog While One is Open
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQT5 : Unable to Create Another Dialog While One is Open
#1
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
Reply
#2
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_())
Reply
#3
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")
Reply
#4
Thank you to the both of you. That helped me work it out. Solved.

Regards
iMu
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there is a printer dialog box in PySimpleGUI shaunfish 2 1,629 Aug-22-2022, 07:29 PM
Last Post: deanhystad
  How a QMainWindow can open a dialog? panoss 4 3,536 Feb-03-2022, 04:33 PM
Last Post: panoss
  How can I create a new tag with PyQt5? nickzsche 2 1,474 Jan-12-2022, 06:10 PM
Last Post: Axel_Erfurt
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 2,230 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  [Tkinter] cancelling open dialog gives empty string rwahdan 2 3,375 Jul-17-2021, 09:17 PM
Last Post: steve_shambles
  Unable to install PyQt5 using pip. edwin4project 2 3,378 Nov-06-2020, 06:53 PM
Last Post: Larz60+
  [WxPython] Return code when closing the dialog ioprst 1 3,178 Aug-13-2019, 11:47 AM
Last Post: jefsummers
  [Tkinter] Unable to create checkbox and select at run time tej7gandhi 5 4,633 May-05-2019, 04:57 PM
Last Post: tej7gandhi
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,820 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [WxPython] Any dialog that allow user to select file OR folder? buran 3 4,223 Apr-03-2019, 06:33 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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