pyqt5单行文本写入
时间: 2023-09-14 07:06:28 浏览: 39
你可以通过以下代码实现在PyQt5中写入单行文本:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout
app = QApplication([])
window = QWidget()
text_input = QLineEdit()
submit_button = QPushButton('Submit')
layout = QVBoxLayout()
layout.addWidget(text_input)
layout.addWidget(submit_button)
window.setLayout(layout)
def submit_text():
text = text_input.text()
# You can do something with the text here
print(text)
submit_button.clicked.connect(submit_text)
window.show()
app.exec_()
```
在这段代码中,创建了一个QLineEdit实例(text_input)以及一个QPushButton实例(submit_button)。将它们添加到QVBoxLayout中,然后将该布局设置为窗口的布局。当用户点击提交按钮时,submit_text函数将被调用,并且可以执行任何所需的操作。在这里,它只打印出文本。
如果你想将文本写入文件,你可以在submit_text函数中使用Python的文件操作API来实现。