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

Question:

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)
Asked By: BroWhat

||

Answers:

Thats the way the pack() geometry manager works. See Tkinter pack method confusion. It’s easier if you use grid():

from tkinter import *

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

question_frame = Frame(window,bg="yellow",width=400,height=450)
question_frame.grid(row=0, column=0)

history_frame = Frame(window,bg="blue",width=400,height=450)
history_frame.grid(row=0, column=1)

Have a look at the old effbot page on grid.

Answered By: figbeam

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)
Answered By: Vladislav Sokolov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.