pyqt界面按钮怎么添加
时间: 2023-11-12 13:02:24 浏览: 104
要在PyQt界面中添加按钮,可以使用以下步骤:
1. 导入PyQt库:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
```
2. 创建一个按钮对象
```python
button = QPushButton('按钮名称', 父窗口对象)
```
3. 设置按钮的位置和大小
```python
button.setGeometry(x坐标, y坐标, 宽度, 高度)
```
4. 将按钮添加到父窗口中
```python
父窗口对象.addWidget(button)
```
完整示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 400, 300)
self.setWindowTitle('PyQt界面按钮示例')
button = QPushButton('点击我', self)
button.setGeometry(50, 50, 100, 30)
self.addWidget(button)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
这将创建一个带有一个按钮的窗口。按钮的位置和大小可以根据需要进行更改。
阅读全文
相关推荐


















