Tkinter

grid() Method in Tkinter Python 3

In this tutorial, we are going to see how to use grid() Method in Tkinter Python 3. This geometry manager organizes widgets in a two-dimensional array. The master widget is divided into a number of rows and columns, and each “cell” of the resulting array can contain a widget.
 

Syntax:
widget.grid(options)

 

 
Here is the list of options:

  • column: The column in which to place the widget; default 0 (left column).
  • columnspan: How many columns the widget occupies; default is 1.
  • ipadx, ipady: How many pixels to fill the widget, horizontally and vertically, inside the widget borders.
  • padx, pady: How many pixels to fill the widget, horizontally and vertically, outside the widget borders.
  • row: The row in which to place the widget; by default the first row that is still empty.
  • rowpan: How many rows the widget occupies; by default 1.
  • sticky: What to do if the cell is bigger than the widget. sticky can be the string concatenation of zero or more of N, E, S, W, NE, NW, SE and SW.

 

Example: grid() Method in Tkinter Python 3
from tkinter import *

gui = Tk()

Label(gui, text="Firstname").grid(row=0)
Label(gui, text="Lastname").grid(row=1)

e1 = Entry(gui)
e2 = Entry(gui)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

gui.mainloop()

 

 
Output:

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *