The Grid Layout Geometry Manager

We look at code that lays out 16 labeled buttons in a planned manner. According to the label on each button, there is only one place it should be within a North, South, East, West reference system.

Getting ready

Both grid and pack have navigation reference schemes. The easiest way to understand in terms of how our GUIs are going to appear is the grid that specifies the positioning of our widget using a clear row, column scheme as illustrated in the following screenshot:

Getting ready

How to do it...

Execute the program shown in the usual manner. The result is shown in the following screenshot:

How to do it...
# grid_button_array_1.py
#>>>>>>>>>>>>>>>>>>>
from Tkinter import *
root = Tk()
root.title("Pack Geometry Manager")
butn_NW = Button(root, bg='blue',text="NorthWest").grid(row=0,  column=0)
butn_NW1 = Button(root, bg='blue',text="Northwest").grid(row=0,  column=1)
butn_NE1 = Button(root, bg='blue',text="Northeast").grid(row=0,  column=2)
butn_NE = Button(root, bg='blue',text="NorthEast").grid(row=0,  column=3)
butn_N1W = Button(root, bg='sky blue',text="norWest").grid(row=1,  column=0)
Grid Layout Geometry Managerexamplebutn_N1W1 = Button(root, bg='sky blue',text="norwest").grid(row=1,  column=1)
butn_S1E1 = Button(root, bg='pale green',text="soueast").grid(row=1, column=2)
butn_S1E = Button(root, bg='pale green',text="souEast").grid(row=1, column=3)
butn_SW = Button(root, bg='green',text="SouthWest").grid(row=2, column=0)
butn_SW1 = Button(root, bg='green',text="SothuWest").grid(row=2,  column=1)
butn_SE1 = Button(root, bg='green',text="SouthEast").grid(row=2,  column=2)
butn_SE = Button(root, bg='green',text="SouthEast").grid(row=2,  column=3)
root.mainloop()

How it works...

The Grid Layout Manager is explicit in interpreting layout instructions. There is no ambiguity and the results are easy to understand. Fortunately, for us users, one of the entrenched philosophies of the Python language is that wherever possible the interpreter should be kind and forgiving to mildly careless programming. For instance, say for example, we assigned the grid layout of all the buttons to the same address. For example, say we assigned all the buttons as grid(row=5, column=5). The result would be what appeared to be a single button inside the window. In fact, the layout manager would place all the buttons on top of one another, with the first one at the bottom and the last one on top. If we destroyed them one at a time in reverse order we would see this sequence unfolding.

There's more...

Just remember that we never mix pack and grid layout managers in the same program. If you do, your program will freeze as each of the managers attempts to obey conflicting instructions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.60.249