220.md 1.5 KB
Newer Older
W
wizardforcel 已提交
1
# QT4 表格
W
wizardforcel 已提交
2 3 4 5 6 7 8

> 原文: [https://pythonspot.com/qt4-table/](https://pythonspot.com/qt4-table/)

我们可以使用 QTableWidget 来显示表格,QTableWidget 是 [PyQt](https://pythonspot.com/pyqt4/) 模块的一部分。 我们设置标题,行数,列数并添加数据。

## Qt4 表示例

W
wizardforcel 已提交
9
下面的例子:
W
wizardforcel 已提交
10

W
wizardforcel 已提交
11
```py
W
wizardforcel 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

def main():
    app 	= QApplication(sys.argv)
    table 	= QTableWidget()
    tableItem 	= QTableWidgetItem()

    # initiate table
    table.setWindowTitle("QTableWidget Example @pythonspot.com")
    table.resize(400, 250)
    table.setRowCount(4)
    table.setColumnCount(2)

    # set data
    table.setItem(0,0, QTableWidgetItem("Item (1,1)"))
    table.setItem(0,1, QTableWidgetItem("Item (1,2)"))
    table.setItem(1,0, QTableWidgetItem("Item (2,1)"))
    table.setItem(1,1, QTableWidgetItem("Item (2,2)"))
    table.setItem(2,0, QTableWidgetItem("Item (3,1)"))
    table.setItem(2,1, QTableWidgetItem("Item (3,2)"))
    table.setItem(3,0, QTableWidgetItem("Item (4,1)"))
    table.setItem(3,1, QTableWidgetItem("Item (4,2)"))

    # show table
    table.show()
    return app.exec_()

if __name__ == '__main__':
    main()

```

结果:

W
wizardforcel 已提交
48 49
![PyQT Table](img/185c656e13f47debbad67f5133a4215d.jpg)

W
wizardforcel 已提交
50 51 52 53 54
![PyQT Table ](img/a60b759f387958b2b4c7046ecd6f4b87.jpg) 

![PyQT Table tooltips](img/f1f56d1db3e8574fad782a6392cbb56c.jpg) 

[PyQT](https://pythonspot.com/pyqt4/) 表格提示
W
wizardforcel 已提交
55 56

[下载 PyQT 代码(批量收集)](https://pythonspot.com/python-qt-examples/)