Tkinter - Grid in Python

Grid allows widgets to be placed at locations in a grid by stating rows and columns. This is a simple example of placing 4 widgets.

#import Tkinter
import tkinter

#create a window
window = tkinter.Tk()

#Sets window title
window.title("Grid Example")

#Set variable to default value 
var1 = tkinter.StringVar(window)
var1.set("text.txt")
var2 = tkinter.IntVar(window)
var2.set(r"C:\temp")

#Create a Label
lbl1 = tkinter.Label(window, text="   File:    ")
lbl1.grid(padx=10, pady=10, row=0, column=0)

#Creates a text input box - set to A
input1 = tkinter.Entry(window,textvariable=var1)
input1.grid(padx=10, pady=10, row=0, column=1)

#Create a Label
lbl2 = tkinter.Label(window, text="   Location:    ")
lbl2.grid(padx=10, pady=10, row=1, column=0)

#Creates a text input box - set to B
input2 = tkinter.Entry(window,textvariable=var2)
input2.grid(padx=10, pady=0, row=1, column=1)

#Loops the tkinter window
window.mainloop()
Creates the window.





Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Must Read Articles

Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter _ Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines and labels.
Tkinter - Pack vs Grid vs Place - tkinter provides flexibility in how the layout the widgets. 
Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.