0

I'm new in Python and PYQT and I'm trying to change the background color of the cells of my Tableview based on the value of the cell. Using some examples I read here, I came up with this code.

def openWindow(self):
    self.window = QtWidgets.QMainWindow()
    self.ui = Ui_Variacao_Diaria()
    self.ui.setupUi(self.window)
    self.window.show()

    file = r'C:/Desktop/Teste_base.xlsx'
    df = pd.read_excel(file)

    df2 = pd.pivot_table(df,index=["Date"],columns=["Club"])
    df3 = pd.DataFrame(df2.to_records())
    headers = list(df3)

    self.ui.t_var.setColumnCount(0)
    for c in range(0,len(df3.columns)):
        self.ui.t_var.insertColumn(c)  
        self.ui.t_var.setRowCount(0)
        for l in range(0,len(df3)):
            self.ui.t_var.insertRow(l)
            for c in range(0,len(df3.iloc[l])):
                self.ui.t_var.setItem(l, c, QTableWidgetItem(str(df3.iloc[l][c])))                    
                self.ui.t_var.setHorizontalHeaderLabels(headers)
                if df3.iloc[l][c] < 100:
                    self.ui.t_var.item(l,c).setBackground(QtGui.QColor(125,125,125)) 

The problem is that when I put this

if df3.iloc[l][c] < 100: self.ui.t_var.item(l,c).setBackground(QtGui.QColor(125,125,125))

my code stops in the first cell and does not fill the rest of my TableView with the data. How can I make to continuous with the looping??

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Execute it in a terminal or a CMD and check the error message, it seems that your IDE is not properly configured to catch the error messages. – eyllanesc May 05 '18 at 16:46
  • Hi eyllanesc! I'm receiving the error message "'NoneType' object has no attibute 'setBackground' ". Any idea about It? Thanks!! – Camila Dragoni May 07 '18 at 21:37

0 Answers0