pyqt5按钮点击发送消息
时间: 2023-08-17 09:05:43 浏览: 158
在PyQt5中,可以通过实现点击事件的响应函数来实现按钮点击发送消息的功能。在项目的main.py文件中,可以添加一个点击事件的响应函数,具体代码如下:
```python
import sys
from PyQt5 import QtWidgets
from click_show_text import Ui_MainWindow
class MyPyQT_Form(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyPyQT_Form, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.push_button_click) # 将按钮的点击事件与响应函数连接起来
def push_button_click(self):
self.textEdit.setText("我点击了按钮,显示框显示出文字") # 在响应函数中设置文本框的显示内容
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
my_pyqt_form = MyPyQT_Form()
my_pyqt_form.show()
sys.exit(app.exec_())
```
在上述代码中,我们首先导入了必要的模块,然后定义了一个名为MyPyQT_Form的类,该类继承自QtWidgets.QMainWindow和Ui_MainWindow。在类的初始化函数中,我们调用了父类的初始化函数,并使用self.setupUi(self)来设置界面。接下来,我们通过self.pushButton.clicked.connect(self.push_button_click)将按钮的点击事件与响应函数push_button_click连接起来。在push_button_click函数中,我们使用self.textEdit.setText来设置文本框的显示内容为"我点击了按钮,显示框显示出文字"。最后,我们创建了一个应用程序对象app,实例化了MyPyQT_Form类,并显示出来。
这样,当按钮被点击时,就会触发push_button_click函数,从而将文本框的显示内容设置为指定的文字。
#### 引用[.reference_title]
- *1* [python的Pyqt5按钮与文本框联动](https://blog.csdn.net/lm_is_dc/article/details/123373487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)