Search code examples
pythontkinterpack

Why can't my Tkinter frames be on the same level


I am testing for a separate project I am working on and in that, I am trying to get two Tkinter frames to be aligned with one another. However, my frames are on two different levels. I have looked at the grid format but I am trying to use the .pack() way as it can do more for the project I am working on. What I am trying to do is have the frames be adjacent from one another.

from tkinter import *

window = Tk()
window.title("test")
window.geometry('800x600')



question_frame = Frame(window,bg="yellow",width=400,height=450)
question_frame.pack(anchor=NW)

history_frame = Frame(window,bg="blue",width=400,height=450)
history_frame.pack(anchor=NE)

Solution

  • Using anchor in the number of elements not divisible by 3 is difficult. Try using side (LEFT and RIGHT respectively). If you need them to be attached to the top, you can use another parent frame.

    from tkinter import *
    
    window = Tk()
    window.title("test")
    window.geometry('800x600')
    
    question_frame = Frame(window,bg="yellow", width=400,height=450)
    question_frame.pack(side=LEFT)
    
    history_frame = Frame(window,bg="blue",width=400,height=450)
    history_frame.pack(side=RIGHT)