使用Tkinter创建显示表

时间:2013-12-30 17:11:06

标签: python tkinter

我在这里找到了一些代码:Creating a table look-a-like Tkinter

我正在尝试修改它以显示值表。我的问题是'标题'正在覆盖其中一行,我似乎无法修复它。 我有一个字典,其中类实例作为值,每个类实例都有自己的行。 在这种情况下,字典中有5个值,但窗口显示4行加上标题行。 基本上我想把第零行作为我的标题, 第1行对应于第0项 第2行与第1项对应 ... 第5行与第4项对应 这是可能的,因为当我尝试添加额外的行时,我得到: IndexError:列表索引超出范围?

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)

1 个答案:

答案 0 :(得分:1)

你有一个逻辑问题。看起来您正在为汽车中的每个项目创建一行,但您忽略了为标题添加额外的行。然后,将第一个(index = 0)行的值设置为标题,覆盖该行中的数据。

您需要为标题创建一个额外的行,并将汽车的所有数据向下移动一行。