pyqt5如何清除QlineEdit中的字符
时间: 2023-07-04 07:10:23 浏览: 44
可以使用 `clear()` 方法来清除 `QLineEdit` 中的字符,示例代码如下:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout
import sys
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.line_edit = QLineEdit()
self.clear_button = QPushButton('Clear')
self.clear_button.clicked.connect(self.line_edit.clear)
layout = QVBoxLayout()
layout.addWidget(self.line_edit)
layout.addWidget(self.clear_button)
self.setLayout(layout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
sys.exit(app.exec_())
```
在上面的代码中,我们创建了一个 `QLineEdit` 和一个 `QPushButton`,当点击 `QPushButton` 时,会调用 `QLineEdit` 的 `clear()` 方法清除其中的字符。