pyqt5获取文本框内容
时间: 2023-08-18 13:08:54 浏览: 194
您可以使用PyQt5的QLineEdit或QTextEdit等控件来获取文本框内容。以下是一个示例代码:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Example')
# 创建文本框和按钮
self.textbox = QLineEdit(self)
self.textbox.move(20, 20)
self.textbox.resize(280, 30)
self.button = QPushButton('获取文本', self)
self.button.move(20, 60)
# 按钮点击事件
self.button.clicked.connect(self.on_click)
self.show()
def on_click(self):
# 获取文本框内容
textboxValue = self.textbox.text()
print(textboxValue)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在上面的示例中,我们创建了一个QLineEdit控件和一个QPushButton控件,并且将它们放置在窗口上。我们通过调用`self.textbox.text()`方法来获取文本框中的内容。当我们点击按钮时,会触发`on_click`方法,并在控制台输出文本框内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)