- PyQt Tutorial
- PyQt - Home
- PyQt - Introduction
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Signals and Slots
- PyQt - Layout Management
- PyQt - Basic Widgets
- PyQt - QDialog Class
- PyQt - QMessageBox
- PyQt - Multiple Document Interface
- PyQt - Drag and Drop
- PyQt - Database Handling
- PyQt - Drawing API
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
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
The following example implements the input dialog functionality. The top level window has three buttons. Their clicked() signal pops up InputDialog through connected slots.
items = ("C", "C++", "Java", "Python") item, ok = QInputDialog.getItem(self, "select input dialog", "list of languages", items, 0, False) if ok and item: self.le.setText(item) def gettext(self): text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter your name:') if ok: self.le1.setText(str(text)) def getint(self): num,ok = QInputDialog.getInt(self,"integer input dualog","enter a number") if ok: self.le2.setText(str(num))
The complete code is as follows −
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class inputdialogdemo(QWidget): def __init__(self, parent = None): super(inputdialogdemo, self).__init__(parent) layout = QFormLayout() self.btn = QPushButton("Choose from list") self.btn.clicked.connect(self.getItem) self.le = QLineEdit() layout.addRow(self.btn,self.le) self.btn1 = QPushButton("get name") self.btn1.clicked.connect(self.gettext) self.le1 = QLineEdit() layout.addRow(self.btn1,self.le1) self.btn2 = QPushButton("Enter an integer") self.btn2.clicked.connect(self.getint) self.le2 = QLineEdit() layout.addRow(self.btn2,self.le2) self.setLayout(layout) self.setWindowTitle("Input Dialog demo") def getItem(self): items = ("C", "C++", "Java", "Python") item, ok = QInputDialog.getItem(self, "select input dialog", "list of languages", items, 0, False) if ok and item: self.le.setText(item) def gettext(self): text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter your name:') if ok: self.le1.setText(str(text)) def getint(self): num,ok = QInputDialog.getInt(self,"integer input dualog","enter a number") if ok: self.le2.setText(str(num)) def main(): app = QApplication(sys.argv) ex = inputdialogdemo() ex.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
The above code produces the following output −