PyQt5 Input Dialog | Python

PyQt5 provides a class named QInputDialog which is used to take input from the user. In most of the application, there comes a situation where some data is required to be entered by the user and hence input dialog is needed. Input can be of type String or Text, Integer, Double and item.

Used methods:
These methods return tuple having two elements – User input and Status, whether user clicked “ok”(true) or “cancel”(false) button after providing desired input.

  1. getText(): This is used to take Text value from the user.
  2. getInt(): This is used to take integer value from the user.
  3. getDouble(): This is used to take Double value from the user.
  4. getItem(): This is used to take the selected item from mulitiple choice by the user.

Example:

Let us create a simple application using QInputDialog where a Main Window will appear having a button “Proceed”. After clicking that button multiple input dialogs will open asking for name, roll, CGPA and learned programming language from a list of languages.



Finally, Main Window will give a confirmation message along with the details provided by user.

Below is the Code –

filter_none

edit
close

play_arrow

link
brightness_4
code

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_()) 

chevron_right


Output:

Click “Proceed” button.

Give the details.

Confirmation message along with user data.



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.