-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay_data_from_table_application
38 lines (32 loc) · 1.33 KB
/
Display_data_from_table_application
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
import sys
from PySide6.QtGui import QColor
from PySide6.QtWidgets import (QApplication, QTableWidget,QTableWidgetItem)
colors = [("Red", "#FF0000"),
("Green", "#00FF00"),
("Blue", "#0000FF"),
("Black", "#000000"),
("White", "#FFFFFF"),
("Electric Green", "#41CD52"),
("Dark Blue", "#222840"),
("Yellow", "#F9E56d")]
def get_rgb_from_hex(code):
code_hex = code.replace("#", "")
rgb = tuple(int(code_hex[i:i+2], 16) for i in (0,2,4))
return QColor.fromRgb(rgb[0], rgb[1], rgb[2])
app = QApplication()
# We configure the table to have the same number of rows and columns as the colors structure
table = QTableWidget()
table.setRowCount(len(colors))
table.setColumnCount(len(colors[0]) + 1) # we use +1 to include a new column where we display the color
table.setHorizontalHeaderLabels(["Name", "Hex Code", "Color"])
# Here we iterate the data structure, create the QTableWidgets instances, and add them into table using x, y coordinate
for i, (name, code) in enumerate(colors):
item_name = QTableWidgetItem(name)
item_code = QTableWidgetItem(code)
item_color = QTableWidgetItem()
item_color.setBackground(get_rgb_from_hex(code))
table.setItem(i, 0, item_name)
table.setItem(i, 1, item_code)
table.setItem(i, 2, item_color)
table.show()
sys.exit(app.exec())