Python 3 PyQt5 Taking User Input Dialog Using Popup Window in QInputDialog Full Tutorial For Beginners

Python 3 PyQt5 Taking User Input Dialog Using Popup Window in QInputDialog Full Tutorial For Beginners

 

Welcome folks today in this post we will be taking user input inside PyQt5 using QInputDialog library in python. All the full source code of the application will be given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install pyqt5

 

After installing it make an app.py file and copy paste the following code

 

app.py

 

 

from PyQt5 import QtCore, QtGui, QtWidgets 
import sys 

class Ui_MainWindow(QtWidgets.QWidget): 
    def setupUi(self, MainWindow): 
        MainWindow.resize(422, 255) 
        self.centralwidget = QtWidgets.QWidget(MainWindow) 

        self.pushButton = QtWidgets.QPushButton(self.centralwidget) 
        self.pushButton.setGeometry(QtCore.QRect(160, 130, 93, 28)) 

        # For displaying confirmation message along with user's info. 
        self.label = QtWidgets.QLabel(self.centralwidget)    
        self.label.setGeometry(QtCore.QRect(170, 40, 201, 111)) 

        # Keeping the text of label empty initially.     
        self.label.setText("")   

        MainWindow.setCentralWidget(self.centralwidget) 
        self.retranslateUi(MainWindow) 
        QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    def retranslateUi(self, MainWindow): 
        _translate = QtCore.QCoreApplication.translate 
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) 
        self.pushButton.setText(_translate("MainWindow", "Proceed")) 
        self.pushButton.clicked.connect(self.takeinputs) 
        
    def takeinputs(self): 
        name, done1 = QtWidgets.QInputDialog.getText( 
            self, 'Input Dialog', 'Enter your name:') 

        roll, done2 = QtWidgets.QInputDialog.getInt( 
        self, 'Input Dialog', 'Enter your roll:') 

        cgpa, done3 = QtWidgets.QInputDialog.getDouble( 
            self, 'Input Dialog', 'Enter your CGPA:') 

        langs =['C', 'c++', 'Java', 'Python', 'Javascript'] 
        lang, done4 = QtWidgets.QInputDialog.getItem( 
        self, 'Input Dialog', 'Language you know:', langs) 

        if done1 and done2 and done3 and done4 : 
            # Showing confirmation message along 
            # with information provided by user. 
            self.label.setText('Information stored Successfully\nName: '
                                +str(name)+'('+str(roll)+')'+'\n'+'CGPA: '
                                +str(cgpa)+'\nSelected Language: '+str(lang)) 

            # Hide the pushbutton after inputs provided by the use. 
            self.pushButton.hide()   
                
            
            
if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv) 
    MainWindow = QtWidgets.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    sys.exit(app.exec_())

 

See also  Python 3 Script to Replace Spaces with New Lines in Text File Full Project For Beginners

 

Here we are asking different questions and collecting user data from popup user input window or dialog boxes in pyqt5.

 

Now if you execute this python script app.py by typing the below command

See also  Python 3 Tkinter Script to Build 10 Digit Mobile Number Generator Using Random Module GUI Desktop App Full Project For Beginners

 

python app.py

 

 

 

Leave a Reply