etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

Python Tkinter Geometry Managers

In this article, you will learn different Python Tkinter Geometry managers. Geometry managers help to position widgets.

Tkinter is the standard GUI library that provides a fast and easy way to create GUI applications. It is also called the Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. The greatest strength of Tkinter is its straightforwardness. It accompanies a wide scope of essential widgets for almost all common tasks, such as text, catches, radio catches, checkboxes, comboboxes, scales, names, or drawing canvas.

Tkinter contains three geometry managers-
  • Pack()
  • Place()
  • Grid()




Tkinter Packer Manager

Tkinter pack() method packs widgets in blocks before placing them in the parent widget. It determines the size and alignment of all your widgets. The process of packer is similar to how we pack elements inside a suitcase when travelling.

Syntax of Pack Manager
widget.pack(option)
from tkinter import *

w = Tk()
w.geometry("200x100")

l1 = Label(w, text="January", bg="#FFB857")
l1.pack()
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack()
l3 = Label(w, text="March", bg="#FF7182")
l3.pack()

mainloop()
Tkinter Geometry Pack

The option in pack() method can be fill, expand and side.





Fill

It controls how to fill the widget space. It fills the space according to the given attribute. It accepts the following attributes-
NONE (Default), X (Fill Horizontally), Y (Fill Vertically) and BOTH (Fill both Horizontally and Vertically)


from tkinter import *

w = Tk()
w.geometry("200x100")

l1 = Label(w, text="January", bg="#FFB857")
l1.pack(fill=X)
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack(fill=X)
l3 = Label(w, text="March", bg="#FF7182")
l3.pack(fill=X)

mainloop()
Tkinter Pack Fill

Expand

The 'Expand' accepts two attributes- YES or NO. It set to YES, the widget expands to fill space not used in parent widget.

from tkinter import *

w = Tk()
w.geometry("200x100")

l1 = Label(w, text="January", bg="#FFB857")
l1.pack(expand=YES)
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack(expand=NO)
l3 = Label(w, text="March", bg="#FF7182")
l3.pack(expand=YES)

mainloop()
Tkinter Pack Expand

Side

Determines which side of parent widget it packs to. It has the following attributes-
TOP(Default), BOTTOM, LEFT, RIGHT

from tkinter import *

w = Tk()
w.geometry("200x100")

l1 = Label(w, text="January", bg="#FFB857")
l1.pack(side=TOP)
l2 = Label(w, text="February", bg="#8EC2C9")
l2.pack(side=RIGHT)
l3 = Label(w, text="March", bg="#FF7182")
l3.pack(side=LEFT)

mainloop()
Tkinter Pack Size



Placer Manager

Tkinter place() method provides the size of the widgets and locations to place them. It organizes all the widgets in a specific position in a parent widget

The options in place() method can be anchor, bordermode, height, width, relheight, relwidth, relx, rely, x and y.


OPTIONS
DESCRIPTIONS
Anchor Indicates the compass directions, where the widget is anchored to, like - N, E, W, S, NE, ES, NW or SW.
Bordermode The Bordermode has two options Inside and Outside. The inside indicates that the other options refer to the parent's inside.
Width, Height It specifies the width and height of widget in pixels.
Relwidth, Relheight It specifies the width and height of the parent widget in fraction, i.e., between 0.0 to 1.0.
Relx, Rely It specifies horizontal and vertical offset as a fraction of the width of parent widget, i.e., between 0.0 to 1.0.
X, Y It specifies horizontal and vertical offset in pixels.




These are the examples of place geometry manager.

Width, Height

from tkinter import *
root = Tk()
root.geometry("200x120")

btn_height = Button(root, text="40px high", bg="#FFB857")
btn_height.place(height=50, x=5, y=20)

btn_width = Button(root, text="80px wide", bg="#FF7182")
btn_width.place(width=80, x=30, y=80)

root.mainloop()
Tkinter Place width height

Relwidth, Relheight

from tkinter import *
root = Tk()
root.geometry("200x100")

btn_height = Button(root, text="0.2 relheight", bg="#FF7182")
btn_height.place(relheight=0.4, x= 5, y = 2)

btn_width = Button(root, text="0.6 relwidth", bg="#BEB9B5")
btn_width.place(relwidth=0.6, x = 20, y = 60)

root.mainloop()
Tkinter Place width height

Relx, Rely, Relx, Rely

from tkinter import *
root = Tk()
root.geometry("200x100")

btn_height = Button(root, text="0.2 relheight", bg="#FF7182")
btn_height.place(relheight=0.4, relx= 0.2, y = 2)

btn_width = Button(root, text="0.6 relwidth", bg="#BEB9B5")
btn_width.place(relwidth=0.6, x = 20, rely = 0.5)

root.mainloop()
Tkinter Place width height



Grid Manager

Tkinter grid() method organises widgets in a table like structure in the parent widget. It is most flexible geometry manager in Tkinter. It splits the parent widget in rows and columns and use the grid method to specify the row and column to place the widget. Syntax of grid manager -

widget.grid(options)

The options in grid() method can be column, columnspan, ipadx, ipady, padx, pady, row, rowspan and sticky.


OPTIONS
DESCRIPTIONS
column Insert a widget in a column. The default column is 0, which is the leftmost column.
columnspan Number of column widget takes up. Default is 1.
ipadx, ipady The pixels to pad widget horizontally and vertically inside the widget's borders.
padx, pady The pixels to pad widget horizontally and vertically outside the widget's borders.
row The row to put widget in. The default is 0.
rowspan The number of rows the widget takes up. The default is 1.
sticky The sticky is used when the widget is smaller than the cell. It Indicates the compass directions, where the widget sticks to, like - N, E, W, S, NE, ES, NW or SW.




These are the examples of grid geometry manager.

Column, Columnspan

from tkinter import *
root = Tk()
root.geometry("200x100")

b1 = Button(root, text="Column 3",bg="#D5FF48")
b1.grid(column=3)

b2 = Button(root, text="Columnspan 3",bg="#B3B1B1")
b2.grid(columnspan=3)

root.mainloop()
Tkinter Grid Column

ipadx, ipady, padx, pady

from tkinter import *
root = Tk()
root.geometry("200x90")

b1 = Button(root, text="ipadx",bg="#07F7AD")
b1.grid(ipadx=6, ipady=4)

b2 = Button(root, text="ipady",bg="#0BDEF2")
b2.grid(ipady=4)

root.mainloop()
Tkinter Grid Ipad

Row, Rowspan

from tkinter import *
root = Tk()
root.geometry("200x100")

b1 = Button(root, text="Row3", bg="#D5FF48")
b1.grid(row=3)

b2 = Button(root, text="Rowspan3", bg="#B3B1B1")
b2.grid(rowspan=3)

root.mainloop()
Tkinter Grid Row

Sticky

from tkinter import *
root = Tk()
root.geometry("200x100")

b1 = Button(root, text="Stick to NE", bg="#07F7AD")
b1.grid(sticky=NE)

root.mainloop()
Tkinter Stick NE



Related Articles

Python Tkinter Combobox Event Binding
Python Tkinter Combobox
Add background image in Python Tkinter
Python Tkinter Text Widget
Countdown clock and timer using Tkinter in Python
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Entry Field Validation in Tkinter Python
Python Tkinter Tutorial with Examples
Python Tkinter Scale Widget
Python3 Tkinter Messagebox
Python 3 Tkinter Menu Bar
Python Tkinter Geometry Managers
Python get visitor information by IP address
Python OpenCV ColorMap
Scientific calculator in Python
Entry Field Validation in Tkinter Python
Python take screenshot of specific window




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP code to send email using SMTP
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to get current directory, filename and code line number in PHP
-----------------
How to add multiple custom markers on google map
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
Fibonacci Series Program in PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
How to generate QR Code in PHP
-----------------
Simple pagination in PHP
-----------------
PHP MYSQL Advanced Search Feature
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP Server Side Form Validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
jQuery File upload progress bar with file size validation
-----------------
Simple File Upload Script in PHP
-----------------
Simple PHP File Cache
-----------------
Php file based authentication
-----------------
To check whether a year is a leap year or not in php
-----------------
Calculate distance between two locations using PHP
-----------------
PHP User Authentication by IP Address
-----------------
PHP Secure User Registration with Login/logout
-----------------
How to print specific part of a web page in javascript
-----------------
Simple way to send SMTP mail using Node.js
-----------------
Simple Show Hide Menu Navigation
-----------------
Detect Mobile Devices in PHP
-----------------
Polling system using PHP, Ajax and MySql
-----------------
PHP Sending HTML form data to an Email
-----------------
Google Street View API Example
-----------------
Get Visitor\'s location and TimeZone
-----------------
SQL Injection Prevention Techniques
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
Convert MySQL to JSON using PHP
-----------------
PHP Programming Error Types
-----------------
CSS Simple Menu Navigation Bar
-----------------
Date Timestamp Formats in PHP
-----------------
Set and Get Cookies in PHP
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
How to display PDF file in web page from Database in PHP
-----------------
Write a python program to print all even numbers between 1 to 100
-----------------
PHP Getting Document of Remote Address
-----------------
File Upload Validation in PHP
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial






Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2023. All Rights Reserved.