本文介绍了PyQt 多行文本输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt 并尝试为用户构建一个多行文本输入框.但是,当我运行下面的代码时,我得到一个只允许输入一行文本的框.如何修复它以便用户可以根据需要输入尽可能多的行?

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()

推荐答案

QLineEdit 是一个提供单行而非多行的小部件.为此,您可以使用 QPlainTextEdit.

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()

这篇关于PyQt 多行文本输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:14