# PyQt5 文本框示例 > 原文: [https://pythonspot.com/pyqt5-textbox-example/](https://pythonspot.com/pyqt5-textbox-example/) 在本文中,您将学习如何在 [**PyQt5**](https://pythonspot.com/pyqt5/) 中使用文本框。 该小部件称为 **QLineEdit** ,并具有 **setText** ()来设置文本框值的方法,以及**文本**()来获取值的方法。 我们可以使用 resize(width,height)方法设置文本框的大小。 可以使用 move(x,y)方法或使用网格布局来设置位置。 **相关课程:** * [使用 PyQt5 创建 GUI 应用](https://gum.co/pysqtsamples) **PyQt5 文本框** 创建文本框非常简单: ``` self.textbox = QLineEdit(self) self.textbox.move(20, 20) self.textbox.resize(280,40) ``` ![pyqt5-QLineEdit](img/4c1c799e1aa4d7f3a9c4b9104ff0efe2.jpg) **PyQt5 文本框示例** 下面的示例创建一个带有文本框的窗口。 ``` import sys from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox from PyQt5.QtGui import QIcon from PyQt5.QtCore import pyqtSlot class App(QMainWindow): def __init__(self): super().__init__() self.title = 'PyQt5 textbox - pythonspot.com' self.left = 10 self.top = 10 self.width = 400 self.height = 140 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Create textbox self.textbox = QLineEdit(self) self.textbox.move(20, 20) self.textbox.resize(280,40) # Create a button in the window self.button = QPushButton('Show text', self) self.button.move(20,80) # connect button to function on_click self.button.clicked.connect(self.on_click) self.show() @pyqtSlot() def on_click(self): textboxValue = self.textbox.text() QMessageBox.question(self, 'Message - pythonspot.com', "You typed: " + textboxValue, QMessageBox.Ok, QMessageBox.Ok) self.textbox.setText("") if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) ``` [下载 PyQT 代码(批量收集)](https://pythonspot.com/python-qt-examples/)