Tkinter grid() Method



This geometry manager organizes widgets in a table-like structure in the parent widget.

Syntax

widget.grid( grid_options )

Here is the list of possible options −

  • column − The column to put widget in; default 0 (leftmost column).

  • columnspan − How many columns widgetoccupies; default 1.

  • ipadx, ipady − How many pixels to pad widget, horizontally and vertically, inside widget's borders.

  • padx, pady − How many pixels to pad widget, horizontally and vertically, outside v's borders.

  • row − The row to put widget in; default the first row that is still empty.

  • rowspan − How many rowswidget occupies; default 1.

  • sticky − What to do if the cell is larger than widget. By default, with sticky='', widget is centered in its cell. sticky may be the string concatenation of zero or more of N, E, S, W, NE, NW, SE, and SW, compass directions indicating the sides and corners of the cell to which widget sticks.

Example

Try the following example by moving cursor on different buttons −

from tkinter import *
root = Tk( )
b=0
   for r in range(6):
      for c in range(6):
         b=b+1
         Button(root, text=str(b),
            borderwidth=1 ).grid(row=r,column=c)
root.mainloop()

This would produce the following result displaying 12 labels arrayed in a 3 x 4 grid −

Tkinter grid Method
python_gui_programming.htm
Advertisements