TkInter: Can't fill a Frame with another widget
<p>First time with TkInter. According to what I've read, you can use Frame widgets to hold other widgets. So, I laid down some preview Frames as such:</p>

<pre><code>import tkinter as tk


root = tk.Tk()

TextFrame = tk.Frame(root, width=200, height=600, bg="red")
TextFrame.pack(side="left")

ListFrame = tk.Frame(root, width=800, height=600, bg="blue")
ListFrame.pack(side="right")
</code></pre>

<p>However, when I add a widget and tell it to fill the Frame, it simply replaces the frame instead of expanding to fill the area. Test code used:</p>

<pre><code>ListBox = tk.Listbox(ListFrame, selectmode="single", bg="#545454", fg="#d9d9d9")
for X in range(20):
    ListBox.insert('end', X)
ListBox.pack(fill='both', expand=True)
</code></pre>

<p>The same test code expands properly if I set the parent to the main 'root' object. Example:</p>

<pre><code>import tkinter as tk


root = tk.Tk()
root.geometry("1000x600")

ListBox = tk.Listbox(root, selectmode="single", bg="#545454", fg="#d9d9d9")
for X in range(20):
    ListBox.insert('end', X)
ListBox.pack(side="right", fill='both', expand=True)

TextBox = tk.Message(root, text="A bunch of text", width=100)
TextBox.pack(side="left", fill="y")
</code></pre>

<p>Am I doing something wrong, here?</p>

<p>It's impossible to say what the right answer is since your question doesn't explain exactly the look and behavior that you're wanting to achieve.</p>

<p>By default, widgets shrink or expand to fit their contents. 99.9% of the time this is exactly the right thing, and lets to UIs that are very responsive to the change (different fonts, different resolutions, different window sizes, etc.). In your case, the listbox doesn't <em>replace</em> the frame. Instead, the frame is simply shrinking to fit the listbox.</p>

<p>It is doing this because you didn't tell it to do anything different. When you placed the frame in the root window, you didn't tell tkinter that you wanted the frame to fill the window. You've just sort-of left it to float at the top, which gives it the freedom to grow or shrink to fit its contents.</p>

<p>By giving proper attributes to <code>TextFrame.pack(side="left")</code> you can have that frame fill the window, and therefore prevent it from shrinking to fit its children.</p>

<pre><code>TextFrame.pack(side="left". fill="both", expand=True)
</code></pre>

<p>Of course, that assumes you want the frame (and thus, the text widget it contains) to fill the window. If you are putting other windows around it, that may change which options you want to use. </p>

python tkinter