0

I found some code here: Creating a table look-a-like Tkinter

and am trying to amend it to display a table of values. My problem is the the 'headings' are overwriting one of the rows and I can't seem to fix it. I have a dictionary with class instances as values and each class instance gets its own row. In this case there are 5 values in the dictionary but the window is showing 4 rows plus the row of headings. Basically I want row zero to be my headings, row 1 to correspond to item 0 row 2 to correspond with item 1 ... row 5 to correspond with item 4 Is this possible given that when I try to add an extra row I get: IndexError: list index out of range?

class DC(Tk): #display cars
    def __init__(self):
        Tk.__init__(self)
        t = SimpleTable(self, len(cars),8)
        t.pack(side="top", fill="x")
        t.set(0,0,"Make")
        t.set(0,1,"Model")
        t.set(0,2,"Km/l")
        t.set(0,3,"Passengers")
        t.set(0,4,"Doors")
        t.set(0,5,"Reg")
        t.set(0,6,"Daily Cost")
        t.set(0,7,"Weekly Cost")
        t.set(0,8,"Weekend Cost")



class SimpleTable(Frame):
    def __init__(self, parent, rows, columns):


        reg_list=cars.keys()
        Frame.__init__(self, parent, background="black")
        self._widgets = []

        #for row in range(rows):
            #current_row = []
            #current_reg=reg_list[row]

        for row in range(1,rows+1): #amended to start at row 1
            current_row = []
            current_reg=reg_list[row-1]

            make = Label(self, text=cars[current_reg].make,borderwidth=0, width=10)
            make.grid(row=row, column=0, sticky="nsew", padx=1, pady=1)
            current_row.append(make)

            model = Label(self, text=cars[current_reg].model,borderwidth=0, width=10)
            model.grid(row=row, column=1, sticky="nsew", padx=1, pady=1)
            current_row.append(model)

            kml = Label(self, text=cars[current_reg].kml,borderwidth=0, width=10)
            kml.grid(row=row, column=2, sticky="nsew", padx=1, pady=1)
            current_row.append(kml)

            passengers = Label(self, text=cars[current_reg].passengers,borderwidth=0, width=10)
            passengers.grid(row=row, column=3, sticky="nsew", padx=1, pady=1)
            current_row.append(passengers)

            doors = Label(self, text=cars[current_reg].doors,borderwidth=0, width=10)
            doors.grid(row=row, column=4, sticky="nsew", padx=1, pady=1)
            current_row.append(doors)

            reg = Label(self, text=cars[current_reg].reg,borderwidth=0, width=10)
            reg.grid(row=row, column=5, sticky="nsew", padx=1, pady=1)
            current_row.append(reg)

            daily = Label(self, text=cars[current_reg].daily_cost,borderwidth=0, width=10)
            daily.grid(row=row, column=6, sticky="nsew", padx=1, pady=1)
            current_row.append(daily)

            weekly = Label(self, text=cars[current_reg].weekly_cost,borderwidth=0, width=10)
            weekly.grid(row=row, column=7, sticky="nsew", padx=1, pady=1)
            current_row.append(weekly)

            weekend = Label(self, text=cars[current_reg].weekend_cost,borderwidth=0, width=10)
            weekend.grid(row=row, column=8, sticky="nsew", padx=1, pady=1)
            current_row.append(weekend)





    self._widgets.append(current_row)

    for column in range(columns):
        self.grid_columnconfigure(column, weight=1)


    def set(self, row, column, value):
        widget = self._widgets[row][column]
        widget.configure(text=value)
Community
  • 1
  • 1
user3120200
  • 15
  • 1
  • 5

1 Answers1

1

You have a logic problem. It looks like you are creating a row for each item in cars, but you are neglecting to add an additional row for the header. Then, you set the value of the first (index=0) row to be the header, overwriting the data in that row.

You need to create an additional row for the header, and move all the data for the cars down one row.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for replying - I have amended the for statement in the class SimpleTable to start at row 1 (code amended above), but the headers are still overwriting one row. I can't understand why. – user3120200 Dec 30 '13 at 18:04
  • @user3120200: because now you are doing row-1 when calling grid. So, the first number is 1, and you are putting that at 1-1, or row 0. You can solve these problems pretty easily by printing out the values of variables, which will let you see they aren't the values you are assuming they are. – Bryan Oakley Dec 30 '13 at 20:56
  • Thanks for your time. I think the problem lies in the original code which I tried to modify - the headers will always overwrite row 0 of the data, which is not what I am looking for. I will have to try something else. – user3120200 Dec 31 '13 at 10:25