qwidget创建一个表格显示数据
时间: 2023-09-21 16:09:23 浏览: 44
好的,您可以通过以下方式使用QWidget创建表格显示数据:
1. 导入必要的模块和库:
```
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
import sys
```
2. 创建一个QTableWidget对象:
```
table = QTableWidget()
```
3. 设置表格的行数和列数:
```
table.setRowCount(num_rows)
table.setColumnCount(num_cols)
```
4. 循环填充表格中的单元格:
```
for i in range(num_rows):
for j in range(num_cols):
item = QTableWidgetItem(str(data[i][j]))
table.setItem(i, j, item)
```
其中,data是您想要显示的数据,num_rows和num_cols分别是数据的行数和列数。
5. 显示表格:
```
table.show()
```
完整的示例代码如下:
```
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem
import sys
app = QApplication(sys.argv)
table = QTableWidget()
table.setRowCount(2)
table.setColumnCount(3)
data = [['1', '2', '3'], ['4', '5', '6']]
for i in range(2):
for j in range(3):
item = QTableWidgetItem(str(data[i][j]))
table.setItem(i, j, item)
table.show()
sys.exit(app.exec_())
```
希望这可以帮助您创建一个表格显示数据!如果您有任何其他问题,请随时问我。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)