Create simple messagebox to report error in input value
-
Hello,
I am completely new to GUI development using Qt and have about 1 year of python programming. I am working on a project that involves connecting to Oracle database and creating a file for processing. If the result of the query returns a null dataset, I want to have a messagebox popup to inform the user that the information they entered is incorrect.
Here is my code so far:
*import sys
import os
import cx_Oracle
import csv
from datetime import datetime as dt
from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton, QFormLayout, QLabel, QMessageBox
from invalid_popup import Ui_invalidPopupclass Form(QDialog):
def init(self, parent=None):
super(Form,self).init(parent)
self.setWindowTitle("Window Title")# Create widgets self.myCourseInput = QLineEdit(self) self.QCourseLabel = QLabel("Enter Course ID: ") self.submitBtn = QPushButton("SUBMIT") self.showdialog = QMessageBox # Create layout and add widgets layout = QFormLayout() layout.addRow(self.QCourseLabel,self.myCourseInput) layout.addRow(self.submitBtn) # Set dialog layout self.setLayout(layout) # Connect submit button to action self.submitBtn.clicked.connect(self.Submit_btn) # Run code to generate files for grade dispute processing def Submit_btn(self): Text = self.myCourseInput.text().upper() # Set any alpha input to uppercase # Connect to Oracle database os.putenv('ORACLE_HOME', <path to oracle home>) os.putenv('LD_LIBRARY_PATH',r"C:\oracle\instantclient_18_3") cnxn = cx_Oracle.connect(database connection info) cursor = cnxn.cursor() self.SQL1 = "select cm.BATCH_UID as EXTERNAL_COURSE_KEY, cm.COURSE_ID, cm.COURSE_NAME, cm.AVAILABLE_IND, cm.ROW_STATUS, ds.BATCH_UID as DATA_SOURCE_KEY from course_main cm, DATA_SOURCE ds where cm.COURSE_ID = :cid and ds.PK1 = cm.DATA_SRC_PK1" cursor.execute(self.SQL1, {'cid': (Text)}) courseResults = cursor.fetchall() if courseResults == []: self.showdialog.show() else: dskCourse = str(courseResults[0][5]) filename = (r"C:\Downloads\\" + dskCourse) gdCourseFile = open(filename,"w"); output=csv.writer(gdCourseFile, delimiter = "|", lineterminator = "\n") header = ('EXTERNAL_COURSE_KEY', 'COURSE_ID', 'COURSE_NAME', 'AVAILABLE_IND', 'ROW_STATUS', 'DATA_SOURCE_KEY', 'NEW_DATA_SOURCE_KEY') output.writerow(header) ctr = 0 for row in courseResults: if row[4] == 2: ctr += 1 rowStatus = 'ENABLED' newRow = list(row) newRow[3] = 'Y' newRow[4] = (str(rowStatus)) #Update the row_status of the course to enable for the grade dispute newRow.append('SYSTEM') output.writerow(newRow) #Create footer for feed file currentDt = dt.now() today = currentDt.strftime('%H:%M:%S %m/%d/%Y') footer = ('***FileFooter', str(ctr), today) output.writerow(footer) return(self.myCourseInput.text()) else: return(self.myCourseInput.text()) gdCourseFile.close() cnxn.commit() cursor.close() cnxn.close() print("Connection closed.") def showdialog(QDialog): msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText("There are no results for the value entered.") msg.setWindowTitle("Invalid Entry") msg.setStandardButtons(QMessageBox.Ok)
Create the Qt Application
app = QApplication(sys.argv)
Create and show the form
form = Form()
form.show()Run the main Qt loop
sys.exit(app.exec())*
-
Hi and welcome to devnet,
You should use the equivalent of the QMessageBox::information static method.
if not courseResults: QMessageBox.information(self, self.tr("Invalid Entry") self.tr("There are no results for the value entered."))
-
It's the method used for application internationalisation/translation. It's not mandatory but I have the habit of making all strings shown to the users ready for translation.