pyqt创建网格布局并加入两个控件
时间: 2023-07-16 11:14:16 浏览: 138
可以按照以下步骤创建网格布局并加入两个控件:
1. 导入 PyQt 模块
```python
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QLineEdit
import sys
```
2. 创建应用程序实例和窗口实例
```python
app = QApplication(sys.argv)
window = QWidget()
```
3. 创建网格布局实例并将其设置为窗口的布局
```python
gridLayout = QGridLayout()
window.setLayout(gridLayout)
```
4. 创建两个控件实例,例如一个标签和一个文本框
```python
label = QLabel('Label')
lineEdit = QLineEdit()
```
5. 将控件添加到网格布局中
```python
gridLayout.addWidget(label, 0, 0)
gridLayout.addWidget(lineEdit, 0, 1)
```
6. 显示窗口
```python
window.show()
sys.exit(app.exec_())
```
完整代码如下:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QLineEdit
import sys
app = QApplication(sys.argv)
window = QWidget()
gridLayout = QGridLayout()
window.setLayout(gridLayout)
label = QLabel('Label')
lineEdit = QLineEdit()
gridLayout.addWidget(label, 0, 0)
gridLayout.addWidget(lineEdit, 0, 1)
window.show()
sys.exit(app.exec_())
```
阅读全文