DEV Community

Cover image for PyQt QInputDialog
threadspeed
threadspeed

Posted on

1 1

PyQt QInputDialog

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

pyqt qinputdialog

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:

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay