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 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(640, 480)
MainWindow.setWindowTitle("MainWindow")
self.centralwidget = QtGui.QWidget(MainWindow)
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.horizontalLayout = QtGui.QHBoxLayout()
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(4)
self.horizontalLayout.addWidget(self.tableWidget)
self.tableWidget_2 = QtGui.QTableWidget(self.centralwidget)
self.tableWidget_2.setColumnCount(4)
self.tableWidget_2.setRowCount(4)
self.horizontalLayout.addWidget(self.tableWidget_2)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
MainWindow.show()
QtCore.QCoreApplication.processEvents()
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.tableWidget.currentItemChanged.connect(self.change_color)
self.populate_table(self.tableWidget, True)
self.populate_table(self.tableWidget_2)
# le premier item est sélectionné par défaut
self.tableWidget_2.item(0, 0).setBackgroundColor(QtGui.QColor(255,255,255))
def populate_table(self, table, color=None):
for c in range(4):
for r in range(4):
obj = QtGui.QTableWidgetItem()
obj.setText(str(256237 + c + r))
table.setItem(r, c, obj)
if color:
table.item(r, c).setBackgroundColor(QtGui.QColor(0,102,255))
def change_color(self, new, old):
row, col = new.row(), new.column()
if old:
old_row, old_col = old.row(), old.column()
self.tableWidget_2.item(old_row, old_col).setBackgroundColor(QtGui.QColor(255,255,255))
self.tableWidget_2.item(row, col).setBackgroundColor(QtGui.QColor(0,102,255))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
sys.exit(app.exec_()) |
Partager