tkinter Error that occured when using list to create label

Viewed 21

I'm studying python lately, and now I'm working on tkinter and firebase. I was making a timeline viewer that can be changed from a manager.

As I was making, I had to make a table of labels that is 5*7 size. At first, I tried to make it like: label12 = Label(...

But I thought that I could make it into list: label[1][2] = Label(...)

I tested it on other code, and the first dimension list worked well, with the configuration working. I could change the text of the label with: label[1].config(text="test)

but, when I used it on the second dimension list, it gave me an error:

AttributeError: 'int' object has no attribute 'config'

This is a part of my code where the error occured. when I changed the text from daylabel using config, it worked well.

def RegistClicked():
day = \['Mon', 'Tue', 'Wed', 'Thu', 'Fri'\]

    dayarray = ["월요일", "화요일", "수요일", "목요일", "금요일"]
    daylabel = [0 for i in range(5)]
    numlabel = [0 for i in range(8)]
    classnumlabel = [[0 for i in range(6)] for j in range(8)]
    for i in range(0, 5):
        daylabel[i] = Label(win, width=10, height=2, text=dayarray[i])
        daylabel[i].grid(row=4, column=i+2)
    for i in range(0, 7):
        numlabel[i] = Label(win, width=10, height=2, text=i+1)
        numlabel[i].grid(row=i+5, column=1)
    for i in range(1, 8):
        for j in range(1, 6):
            classnumlabel[i][j] = Label(win, width=10, text="value")
            classnumlabel[i][j].grid(row=i+4, column=j+1)
    
    global selectclass
    selectclass = strvar.get()
    classlabel.config(text=selectclass)
    
    for i in range(5):
        for j in range(1, 8):
            dir = db.reference(selectclass +'/'+ day[i] +'/'+ 'class' + str(j))
            print(selectclass +'/'+ day[i] +'/'+ 'class' + str(j))
            classnumlabel[j][i].config(text=dir.get())

Everything was well until I hit the refresh button, which triggers the function above, would kill the program with the error saying that the dayclasslabel array is an 'int' object.

1 Answers

Your bug is caused by trying to call config on a zero integer.

Instead of "predefining" the lists full of zeroes, just append into them.

Additionally, don't use "magic numbers" and ranges such as (0, 5), (0, 7), then suddenly (1, 8) and (1, 6), but name the values (or better yet, in the case of days you can just enumerate the list of day names.

You might be looking for something like this...

from tkinter import Label, Tk


def build_class_win(class_count):
    win = Tk()

    selectclass = '...'
    day_names = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']

    day_labels = []
    num_labels = []
    class_num_labels = {}

    # Build day labels

    for day_index, day_name in enumerate(day_names):
        day_label = Label(win, width=10, height=2, text=day_name)
        day_label.grid(row=0, column=day_index + 1)
        day_labels.append(day_label)

    # Build class labels

    for class_index in range(class_count):
        num_label = Label(win, width=10, height=2, text=class_index + 1)
        num_label.grid(row=1 + class_index, column=0)
        num_labels.append(num_label)

    # Build mapping of day/class labels 

    for day_index, day_name in enumerate(day_names):
        for class_index in range(class_count):
            reference = f'{selectclass}/{day_name}/class{class_index}'
            cls_label = Label(win, width=10, text=reference)
            cls_label.grid(row=class_index + 1, column=day_index + 1)
            class_num_labels[(day_index, class_index)] = cls_label
            # TODO: add firebase stuff here
    return win


if __name__ == '__main__':
    win = build_class_win(class_count=7)
    win.mainloop()

The window looks like

enter image description here

Related