使用pyqt5完成一段代码,在大小为resize(800, 500) 的最底部 创建两个文本框,可以读取到文本框的数据,并且将他们发送出去
时间: 2023-05-30 19:07:48 浏览: 65
以下是使用pyqt5完成的代码,文本框可以读取到数据并将其发送出去:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.resize(800, 500)
self.setWindowTitle("My Widget")
vbox = QVBoxLayout()
self.setLayout(vbox)
label1 = QLabel("Enter Text 1:")
self.textbox1 = QLineEdit()
vbox.addWidget(label1)
vbox.addWidget(self.textbox1)
label2 = QLabel("Enter Text 2:")
self.textbox2 = QLineEdit()
vbox.addWidget(label2)
vbox.addWidget(self.textbox2)
button = QPushButton("Send")
button.clicked.connect(self.send_data)
vbox.addWidget(button)
def send_data(self):
data1 = self.textbox1.text()
data2 = self.textbox2.text()
print("Data 1:", data1)
print("Data 2:", data2)
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
```
在这个例子中,我们创建了一个继承自QWidget的自定义窗口类MyWidget。在构造函数中,我们使用QVBoxLayout创建了一个垂直布局,并添加了两个标签和两个文本框。我们还创建了一个按钮,当用户单击按钮时,我们将调用send_data函数来读取文本框的内容并将其打印到控制台上。
在send_data函数中,我们使用textbox.text()方法来读取文本框的内容。我们将数据1和数据2存储在变量data1和data2中,并使用print语句将它们打印到控制台上。在实际应用中,我们可以将这些数据发送到网络或保存到文件中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)