pyqt5实现按钮点击功能
时间: 2024-02-22 16:33:47 浏览: 73
在 Pyqt5 中实现按钮点击功能,一般需要以下步骤:
1. 导入需要的模块:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot
import sys
```
2. 创建应用程序和窗体对象:
```python
app = QApplication(sys.argv)
window = QWidget()
```
3. 创建按钮对象:
```python
button = QPushButton('点击我', window)
```
4. 创建按钮的点击事件处理函数:
```python
@pyqtSlot()
def on_click():
print('按钮被点击了')
```
5. 将按钮的点击事件与处理函数绑定:
```python
button.clicked.connect(on_click)
```
6. 显示窗体和运行应用程序:
```python
window.show()
sys.exit(app.exec_())
```
完整代码如下:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot
import sys
app = QApplication(sys.argv)
window = QWidget()
button = QPushButton('点击我', window)
@pyqtSlot()
def on_click():
print('按钮被点击了')
button.clicked.connect(on_click)
window.show()
sys.exit(app.exec_())
```
阅读全文