Tuesday, March 29, 2022

Datagrid with PyQt6

This demo program shows how to create a datagrid, place it on a specific location in a window, among other things. Output:
The code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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
import sys
from PyQt6.QtWidgets import QApplication,  QWidget,  QTableWidget
from PyQt6.QtGui import  QPainter, QColor, QPen
from PyQt6.QtCore import Qt

class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()

        self.initUI()

    def initUI(self):


        self.setGeometry(25, 45, 330, 325)
        self.setWindowTitle('Post 4')
        self.createTable()
        self.show()
        
 
    def createTable(self):
        self.tableWidget = QTableWidget(self)
        self.tableWidget.setRowCount(24)
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setFixedSize(280, 275)
        self.tableWidget.move(25, 25)
        self.tableWidget.setHorizontalHeaderLabels(['PM2.5', 'PM10'])
        self.tableWidget.horizontalHeader().setStretchLastSection(True)
        self.tableWidget.verticalHeader().setStretchLastSection(True)

 
def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

No comments:

Post a Comment