PyQt - QInputDialog Widget



This is a preconfigured dialog with a text field and two buttons, OK and Cancel. The parent window collects the input in the text box after the user clicks on Ok button or presses Enter.

The user input can be a number, a string or an item from the list. A label prompting the user what he should do is also displayed.

The QInputDialog class has the following static methods to accept input from the user −

Sr.No. Methods & Description
1

getInt()

Creates a spinner box for integer number

2

getDouble()

Spinner box with floating point number can be input

3

getText()

A simple line edit field to type text

4

getItem()

A combo box from which user can choose item

Example 1: Text Input

In this example we are developing a PyQt6 program showing a QPushButton and a QLineEdit. Upon clicking the button it activates the getText function prompting a dialog, for the user to input text. The text entered is subsequently shown in the QLineEdit field.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLineEdit, QInputDialog

class TextInputExample(QWidget):
   def __init__(self):
      super().__init__()

      self.initUI()

   def initUI(self):
      layout = QVBoxLayout()

      self.btn = QPushButton('Get Text', self)
      self.btn.clicked.connect(self.getText)
      layout.addWidget(self.btn)

      self.text_edit = QLineEdit(self)
      layout.addWidget(self.text_edit)

      self.setLayout(layout)

   def getText(self):
      text, okPressed = QInputDialog.getText(self, "Get Text", "Enter your name:", QLineEdit.Normal, "")
      if okPressed and text != '':
         self.text_edit.setText(text)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = TextInputExample()
   ex.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

pyqt qinputdialog example 1

Example 2: Item Selection

In this example, we provide a list of items ('Red', 'Green', 'Blue') and use QInputDialog to prompt the user to select one of them. The selected item is then displayed in a QLineEdit.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLineEdit, QInputDialog

class ItemSelectionExample(QWidget):
   def __init__(self):
      super().__init__()

      self.initUI()

   def initUI(self):
      layout = QVBoxLayout()

      self.btn = QPushButton('Choose Color', self)
      self.btn.clicked.connect(self.getColor)
      layout.addWidget(self.btn)

      self.color_edit = QLineEdit(self)
      layout.addWidget(self.color_edit)

      self.setLayout(layout)

   def getColor(self):
      items = ['Red', 'Green', 'Blue']
      item, okPressed = QInputDialog.getItem(self, "Select Color", "Choose a color:", items, 0, False)
      if okPressed and item:
         self.color_edit.setText(item)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = ItemSelectionExample()
   ex.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

pyqt qinputdialog example 2
Advertisements