PyQt QInputDialog
Tags: python
Published: 2020-05-22 10:24:14

With PyQt you can create a QInputDialog (graphical input dialog). PyQT is a <a href="https://pythonprogramminglanguage.com/pyqt/">GUI module</a> for <a href="https://python.org">Python</a>. It's a binding for the popular <a href="https://qt.io">Qt</a> framework.

A dialog without input is called a <a href="https://pythonspot.com/pyqt5-messagebox/">messagebox</a> (QMessageBox).

The input dialog typically consists of a <a href="https://pythonbasics.org/qlineedit/">text box</a> and two <a href="https://pythonpyqt.com/pyqt-button/">buttons</a>, the user clicks OK (or Enter), the Dialog collects the input data and returns.

The available dialogs are:

* QInputDialog.getText One line of text
* QInputDialog.getMultiLineText multi-line text
* QInputDialog.getDouble floating point
* QInputDialog.getInt Integer
* QInputDialog.getItem entry selection

![pyqt qinputdialog](https://dev-to-uploads.s3.amazonaws.com/i/t3zvc6hyzuamrp9zkouq.png)

## QInputDialog example

The example below creates various input dialogs. It includes the single line, multi line, double/float input, integer input and a select box.

It shows them one by one in series, not all at once. You have to click an option to show the next dialog.

```python
import sys                                                                                                                                 
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QDialog, QInputDialog                                                      
from PyQt5.Qt import Qt                                                                                                                    
                                                                                                                                           
class App(QWidget):                                                                                                                        
    def __init__(self):                                                                                                                    
        super().__init__()                                                                                                                 
        # text input                                                                                                                                    
        text, ok = QInputDialog.getText(self, 'getText', 'Enter text')                                                                     
        if ok and text:                                                                                                                    
            print(text)                                                                                                                    
                    
        # multi-line input                                                                                                                       
        text, ok = QInputDialog.getMultiLineText(self, 'getMultiLineText', 'Story', "Enter story")                                         
        if ok and text:                                                                                                                    
            print(text)                                                                                                                    
                          
        # enter double                                                                                                                 
        double, ok = QInputDialog.getDouble(self, 'getDouble', 'Enter double', 22.33, -10000, 10000, 2)                                    
        if ok:                                                                                                                             
            print(double)                                                                                                                  
                                                                                                                                           
        # enter integer                                                                                                                                   
        int, ok = QInputDialog.getInt(self, 'getInteger', 'Enter number', 25, 0, 100, 1)                                                   
        if ok:                                                                                                                             
            print(int)                                                                                                                     
        
        # select option                                                                                                                                   
        items = ["Spring", "Summer", "Fall", "Winter"]                                                                                     
        item, ok = QInputDialog.getItem(self, 'getItem', 'Favourite season', items, 0, False)                                              
                                                                                                                                           
        if ok and item:                                                                                                                    
            print(item)                                                                                                                    
                                                                                                                                           
if __name__ == '__main__':                                                                                                                 
    app = QApplication(sys.argv)                                                                                                           
    ex = App()                                                                                                                             
    sys.exit(app.exec_())   
```

**Related links:**
* <a href="https://gumroad.com/l/pysqtsamples">PyQt course and book</a>
* <a href="https://pythonpyqt.com/">PyQt articles</a>