PyQt QInputDialog
threadspeed
Posted on May 22, 2020
With PyQt you can create a QInputDialog (graphical input dialog). PyQT is a GUI module for Python. It's a binding for the popular Qt framework.
A dialog without input is called a messagebox (QMessageBox).
The input dialog typically consists of a text box and two buttons, 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
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.
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:
💖 💪 🙅 🚩
threadspeed
Posted on May 22, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.