Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tkinter

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!

like image 707
Python_user Avatar asked Jan 22 '15 13:01

Python_user


People also ask

What is fill in Tkinter?

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.

What does expand true do in Tkinter?

Whenever we use expand(boolean) property, then we are generally resizing the widget size to expand in its available space. It takes boolean values as true or false. When the expand property is true for the widget, it means we can enable the grow property.

What do you understand by pack method mention the three options of pack?

Python | pack() method in Tkinter Tkinter literally packs all the widgets one after the other in a window. We can use options like fill, expand, and side to control this geometry manager.

What is the difference between pack and place in Tkinter?

pack() organizes widgets in horizontal and vertical boxes that are limited to left, right, top, bottom positions. Each box is offset and relative to each other. place() places widgets in a two dimensional grid using x and y absolute coordinates.


1 Answers

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

like image 176
fhdrsdg Avatar answered Sep 20 '22 11:09

fhdrsdg