Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QMessageBox does not open and throws an error
Qt 6.11 is out! See what's new in the release blog

QMessageBox does not open and throws an error

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PyGrrl_FL
    wrote on last edited by
    #1

    Hi all,

    I am trying to get a dialog box to open and it will not. It throws the following error: TypeError: setWindowTitle(self, str): first argument of unbound method must have type 'QMessageBox'

    This is the code I am trying to run:

    import sys
    import pyodbc
    from datetime import datetime as dt
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QPalette
    from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QFormLayout, QPushButton, QMessageBox, QLabel
    
    class StartForm(QDialog):
        def __init__(self, parent=None):
            super(StartForm,self).__init__(parent)
            self.setGeometry(50, 50, 150, 150)
            self.setWindowTitle("TERM TEMPLATE DATABASE INSERT")
    
            # Create widgets
            self.myCourseCodeInput = QLineEdit(self)
            self.QCourseCodeLabel = QLabel("Enter the Course Code for new template: ")
            self.myTermCodeInput = QLineEdit(self)
            self.QTermCodeLabel = QLabel("Enter the term code (yymmdd) for the new template: ")
            self.startTermTemplateInsertBtn = QPushButton("PROCESS NEW TERM TEMPLATE")
            self.cancelBtn = QPushButton("EXIT APPLICATION")
    
            # Create layout and add widgets
            layout = QFormLayout()
            layout.addRow(self.QCourseCodeLabel,self.myCourseCodeInput)
            layout.addRow(self.QTermCodeLabel,self.myTermCodeInput)
            layout.addRow(self.startTermTemplateInsertBtn, self.cancelBtn)
    
            # Set dialog layout
            self.setLayout(layout)
            self.myCourseCodeInput.setEnabled(True)
            self.myTermCodeInput.setEnabled(True)
            self.startTermTemplateInsertBtn.setEnabled(True)
    
            # Connect buttons and checkbox to actions
            self.startTermTemplateInsertBtn.clicked.connect(self.termInfoVerify)
    
        def templateData(self, appTerm, cursor):
            cid = self.myCourseCodeInput.text().upper()
            appTerm = self.myTermCodeInput.text()
            termDate = dt.strptime(appTerm, '%y%m%d') #Put term input in python datetime format
            tDate = termDate.strftime('%y%m%d') #Convert python format term date to UMA term code format
            xTermDate = termDate.strftime('%Y-%m-%d') #Expand term date to yyyy-mm-dd format
            termTemplate = 'TEMPLATE_' + cid + '_' + tDate
            self.termInfoVerify()
    
        def termInfoVerify(self):
            cidText = self.myCourseCodeInput.text().upper()
            termDate = self.myTermCodeInput.text()    
            QMessageBox.Question()
            QMessageBox.setWindowTitle(self, "Template Info Verification")
            QMessageBox.setText(self, "You entered the following term template information:")
            QMessageBox.setDetailedText(self, "Course Code:" + cidText)
            QMessageBox.setDetailedText(self, "Term Date: " + termDate)
            QMessageBox.setInformativeText(self, "Click 'OK' to process the information.  Click 'Cancel' to make corrections.")
            QMessageBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    
    
    # Create the Qt Application
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    palette = QPalette()
    palette.setColor(QPalette.ButtonText, Qt.blue)
    app.setPalette(palette)
    
    # Create and show the form
    form = StartForm()
    form.show()
    
    # Run the main Qt loop
    sys.exit(app.exec())
    

    Any help is greatly appreciated!

    jsulmJ 1 Reply Last reply
    0
    • P PyGrrl_FL

      Hi all,

      I am trying to get a dialog box to open and it will not. It throws the following error: TypeError: setWindowTitle(self, str): first argument of unbound method must have type 'QMessageBox'

      This is the code I am trying to run:

      import sys
      import pyodbc
      from datetime import datetime as dt
      from PyQt5.QtCore import Qt
      from PyQt5.QtGui import QPalette
      from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QFormLayout, QPushButton, QMessageBox, QLabel
      
      class StartForm(QDialog):
          def __init__(self, parent=None):
              super(StartForm,self).__init__(parent)
              self.setGeometry(50, 50, 150, 150)
              self.setWindowTitle("TERM TEMPLATE DATABASE INSERT")
      
              # Create widgets
              self.myCourseCodeInput = QLineEdit(self)
              self.QCourseCodeLabel = QLabel("Enter the Course Code for new template: ")
              self.myTermCodeInput = QLineEdit(self)
              self.QTermCodeLabel = QLabel("Enter the term code (yymmdd) for the new template: ")
              self.startTermTemplateInsertBtn = QPushButton("PROCESS NEW TERM TEMPLATE")
              self.cancelBtn = QPushButton("EXIT APPLICATION")
      
              # Create layout and add widgets
              layout = QFormLayout()
              layout.addRow(self.QCourseCodeLabel,self.myCourseCodeInput)
              layout.addRow(self.QTermCodeLabel,self.myTermCodeInput)
              layout.addRow(self.startTermTemplateInsertBtn, self.cancelBtn)
      
              # Set dialog layout
              self.setLayout(layout)
              self.myCourseCodeInput.setEnabled(True)
              self.myTermCodeInput.setEnabled(True)
              self.startTermTemplateInsertBtn.setEnabled(True)
      
              # Connect buttons and checkbox to actions
              self.startTermTemplateInsertBtn.clicked.connect(self.termInfoVerify)
      
          def templateData(self, appTerm, cursor):
              cid = self.myCourseCodeInput.text().upper()
              appTerm = self.myTermCodeInput.text()
              termDate = dt.strptime(appTerm, '%y%m%d') #Put term input in python datetime format
              tDate = termDate.strftime('%y%m%d') #Convert python format term date to UMA term code format
              xTermDate = termDate.strftime('%Y-%m-%d') #Expand term date to yyyy-mm-dd format
              termTemplate = 'TEMPLATE_' + cid + '_' + tDate
              self.termInfoVerify()
      
          def termInfoVerify(self):
              cidText = self.myCourseCodeInput.text().upper()
              termDate = self.myTermCodeInput.text()    
              QMessageBox.Question()
              QMessageBox.setWindowTitle(self, "Template Info Verification")
              QMessageBox.setText(self, "You entered the following term template information:")
              QMessageBox.setDetailedText(self, "Course Code:" + cidText)
              QMessageBox.setDetailedText(self, "Term Date: " + termDate)
              QMessageBox.setInformativeText(self, "Click 'OK' to process the information.  Click 'Cancel' to make corrections.")
              QMessageBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
      
      
      # Create the Qt Application
      app = QApplication(sys.argv)
      app.setStyle('Fusion')
      palette = QPalette()
      palette.setColor(QPalette.ButtonText, Qt.blue)
      app.setPalette(palette)
      
      # Create and show the form
      form = StartForm()
      form.show()
      
      # Run the main Qt loop
      sys.exit(app.exec())
      

      Any help is greatly appreciated!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @PyGrrl_FL said in QMessageBox does not open and throws an error:

      setWindowTitle

      This is not a static method! question() is static.
      You need to create a QMessageBox instance and then show it using exec():

      msg = QMessageBox()
      msg.setWindowTitle(self, "Template Info Verification")
      msg.setText(self, "You entered the following term template information:")
      msg.setDetailedText(self, "Course Code:" + cidText)
      msg.setDetailedText(self, "Term Date: " + termDate)
      msg.setInformativeText(self, "Click 'OK' to process the information.  Click 'Cancel' to make corrections.")
      msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
      msg.exec()
      

      If you want to use static question() method then please check its documentation and pass the parameters in the correct way:

      QMessageBox.question(self, "Template Info Verification", ...)
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • P Offline
        P Offline
        PyGrrl_FL
        wrote on last edited by
        #3

        Thanks, jsulm! I see now where my mistake is. I was mixing things around. I was able to get it working with your guidance.

        aha_1980A 1 Reply Last reply
        1
        • P PyGrrl_FL

          Thanks, jsulm! I see now where my mistake is. I was mixing things around. I was able to get it working with your guidance.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi @PyGrrl_FL,

          Glad you solved it. So please mark this topic SOLVED too. Thanks!

          Qt has to stay free or it will die.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved