Difference between "fill" and "expand" options for tkinter pack method

PythonTkinter

Python Problem Overview


I know this is a too trivial question, but I am new to python, and I have just started using the tkinter module. I have actually looked up about it everywhere, and I am unable to find the satisfactory answer. I found the following:

> fill option: it determines whether to use up more space or keep > "one's own" dimensions.

> expand option: it deals with the expansion of parent widget.

The problem is that these two sound more or less the same. I even tried out a few examples by toggling between the 4 values of fill and 2 values of expand but received more or less the same output in 2 or 3 cases, because of which I have this query. Any help would be appreciated in this regards. Thanks in advance!

Python Solutions


Solution 1 - Python

From effbot:

>The fill option tells the manager that the widget wants fill the entire space assigned to it. The value controls how to fill the space; BOTH means that the widget should expand both horizontally and vertically, X means that it should expand only horizontally, and Y means that it should expand only vertically. > >The expand option tells the manager to assign additional space to the widget box. If the parent widget is made larger than necessary to hold all packed widgets, any exceeding space will be distributed among all widgets that have the expand option set to a non-zero value.

So fill tells the widget to grow to as much space is available for it in the direction specified, expand tells the master to take any space that is not assigned to any widget and distribute it to all widgets that have a non-zero expand value.

The difference becomes clear when running this example:

import Tkinter as tk

root = tk.Tk()
root.geometry('200x200+200+200')

tk.Label(root, text='Label', bg='green').pack(expand=1, fill=tk.Y)
tk.Label(root, text='Label2', bg='red').pack(fill=tk.BOTH)

root.mainloop()

You can see that the label with expand=1 gets assigned as much space as available for it, but only occupies it in the direction specified, Y. The label with fill=tk.BOTH expands in both directions, but has less space available.

Expand vs Fill

Solution 2 - Python

I'm done with trial and error. Here is an overview:

fill and side combinations with expand = False fill and side combinations with expand = True

import tkinter as tk

root = tk.Tk()
root.geometry()

for e, expand in enumerate([False, True]):
    for f, fill in enumerate([None, tk.X, tk.Y, tk.BOTH]):
        for s, side in enumerate([tk.TOP, tk.LEFT, tk.BOTTOM, tk.RIGHT]):
            position = '+{}+{}'.format(s * 205 + 100 + e * 820, f * 235 + 100)
            win = tk.Toplevel(root)
            win.geometry('200x200'+position)
            text = str("side='{}'\nfill='{}'\nexpand={}".format(side, fill, str(expand)))
            tk.Label(win, text=text, bg=['#FF5555', '#55FF55'][e]).pack(side=side, fill=fill, expand=expand)
                
root.mainloop()

Solution 3 - Python

I'm also a man who struggled to understand how those options work together.

Thankfully, finally got clear understanding with Ronald's visual explanation.

So I made some program to simulate how pack works.
https://github.com/thom-jp/tkinter_pack_simulator

Here is the image of running program:

Here is the image of running program

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPython_userView Question on Stackoverflow
Solution 1 - PythonfhdrsdgView Answer on Stackoverflow
Solution 2 - PythonRonaldView Answer on Stackoverflow
Solution 3 - Pythonthom-jpView Answer on Stackoverflow