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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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_()) |
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
python app.py