Search code examples
pythontkinterframes

Frame list with Tkinter and python


well i want to do a frame list with some info i get from a db what i trying to do is something like

frame1 = Frame(root)
frame2 = Frame(frame1)
frame3 = Frame(frame1)

cont=0
for i in range(2):

    #add some widgets to the frames

    frame2.grid(row=0,column = 0)
    frame2.grid(row=1,column = 0)
    frame3.grid(row=cont,column=0)
    cont += 1

expecting that frames stack below of the other but they are printing in the same place, im actually trying to learn python and english too haha. hope someone can explain me what im doing wrong or a better way to do this

thanks.


Solution

  • Let's make a little data structure to hold your frames, instead of just using names. The data structure will end up looking like this:

    [[frame1_1, frame2_1, frame3_1], [frame1_2, frame2_2, frame3_2]]
    

    Here's the code to accomplish that:

    frameGroup = []
    
    for i in range(2):
    
        frameGroup.append([None, None, None])
        frameGroup[i][0] = Frame(root)
        frameGroup[i][1] = Frame(frameGroup[i][0])
        frameGroup[i][2] = Frame(frameGroup[i][0])
    
        #add some widgets to the frames
    
        frameGroup[i][0].grid(row=i, column=0)
        frameGroup[i][1].grid(row=0, column=0)
        frameGroup[i][2].grid(row=1, column=0)