Search code examples
pythonpyqtpyqt4pyqt5

PyQt Multiline Text Input Box


I am working with PyQt and am attempting to build a multiline text input box for users. However, when I run the code below, I get a box that only allows for a single line of text to be entered. How to I fix it so that the user can enter as many lines as necessary?

   import sys
   from PyQt4.QtGui import *
   from PyQt4.QtCore import *

   def window():
       app = QApplication(sys.argv)
       w = QWidget()

       w.resize(640, 480)

       textBox = QLineEdit(w)
       textBox.move(250, 120)

       button = QPushButton("click me")
       button.move(20, 80)

       w.show()

       sys.exit(app.exec_())


   if __name__ == '__main__':
       window()

Solution

  • QLineEdit is a widget that provides a single line, not multiline. You can use QPlainTextEdit for that purpose.

    import sys
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
    def window():
        app = QApplication(sys.argv)
        w = QWidget()
    
        w.resize(640, 480)
    
        textBox = QPlainTextEdit(w)
        textBox.move(250, 120)
    
        button = QPushButton("click me", w)
        button.move(20, 80)
    
        w.show()
    
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        window()