Best post:Dimensions, Anchors, Bitmaps & Cursors in Python

Dimensions, Anchors, Bitmaps and Cursors are standard attributes and properties of widgets in tkinter.

There are few other standard attributes and properties of widgets explained earlier like:
Fonts
Colours
Relief Styles

Refer post :
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Events and Bindings in Python along with Widget configuration and styling

Dimensions

A widget will have length, height, width and other dimensions.
This dimensions will have different units.
If dimension is having numeric value it is considered to be in pixels.

  • c : centimeters
  • i : inches
  • m : millimeters
  • p : Printer’s points (about 1/72″)

Various options of length

  • borderwidth : It specifies the width of the border which gives a three-dimensional look to the widget
  • height : It specifies the required height for a widget and it must be greater than or equal to 1.
  • highlightthickness : It specifies the width of the highlight rectangle when the widget has focus.
  • padx pady : padx is used for external padding in X direction / horizontal padding. pady is used for external padding in Y direction / vertical padding.
  • selectborderwidth : It specifies the width of the three-dimentional border around selected items of the widget.
  • underline : Index of the character to underline in the widget’s text i.e. 0 index indicates the first character.
  • width : It specifies the required width for a widget
  • wraplength : It specifies maximum line length for widgets that perform word wrapping.

Anchors

A widget will have to be placed at certain position on the screen relative to the reference point.

Values possible for anchor attribute

NWNNE
WCENTERE
SWSSE

  • If you use CENTER as a text anchor, the text will be centered horizontally and vertically around the reference point.
  • Anchor NW will position the text so that the reference point coincides with the northwest (top left) corner of the box containing the text.
  • Anchor W will center the text vertically around the reference point, with the left edge of the text box passing through that point, and so on.
  • If you create a small widget inside a large frame and use the anchor=SE option, the widget will be placed in the bottom right corner of the frame.
  • If you used anchor=N instead, the widget would be centered along the top edge.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date : 9th April 2020

Description : Program to show use of anchor attribute

"""
from tkinter import *

top = Tk()
L1 = Label(top, text = "ITVoyagers in NW",height=10,width=20,bd=2,anchor=NW,font="verdana",fg="white",bg="purple")
L1.grid(row=0,column=0)

L2 = Label(top,text = "ITVoyagers in W",height=10,width=20,bd=2,anchor=W,font="verdana",fg="pink",bg="black")
L2.grid(row=1,column=0)

L3 = Label(top,text = "ITVoyagers in SW",height=10,width=20,bd=2,anchor=SW,font="verdana",fg="white",bg="purple")
L3.grid(row=2,column=0)

L4 = Label(top, text = "ITVoyagers in N",height=10,width=20,bd=2,anchor=N,font="verdana",fg="pink",bg="black")
L4.grid(row=0,column=1)

L5 = Label(top,text = "ITVoyagers in CENTER",height=10,width=20,bd=2,anchor=CENTER,font="verdana",fg="white",bg="purple")
L5.grid(row=1,column=1)

L6 = Label(top,text = "ITVoyagers in S",height=10,width=20,bd=2,anchor=S,font="verdana",fg="pink",bg="black")
L6.grid(row=2,column=1)

L7 = Label(top, text = "ITVoyagers in NE",height=10,width=20,bd=2,anchor=NE,font="verdana",fg="white",bg="purple")
L7.grid(row=0,column=2)

L8 = Label(top,text = "ITVoyagers in E",height=10,width=20,bd=2,anchor=E,font="verdana",fg="pink",bg="black")
L8.grid(row=1,column=2)

L9 = Label(top,text = "ITVoyagers in SE",height=10,width=20,bd=2,anchor=SE,font="verdana",fg="white",bg="purple")
L9.grid(row=2,column=2)


OUTPUT

Output of anchor attribute in Python (itvoyagers.in)
Output of anchor attribute in Python (itvoyagers.in)

Bitmaps

Bitmap attribute is used to display various bitmaps.

There are following type of bitmaps available −

“error”
“gray75”
“gray50”
“gray25”
“gray12”
“hourglass”
“info”
“questhead”
“question”
“warning”

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date : 9th April 2020

Description : Program to show use of bitmap attribute

"""
from tkinter import *
root=Tk()
Label(root,text="hourglass",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="hourglass",bg="white").pack()

Label(root,text="error",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="error",bg="red").pack()

Label(root,text="question",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="question",bg="pink").pack()

Label(root,text="questhead",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="questhead",bg="sky blue").pack()

Label(root,text="warning",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="warning",bg="yellow").pack()

Label(root,text="info",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="info",bg="cyan").pack()

Label(root,text="gray12",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="gray12",bg="blue").pack()

Label(root,text="gray25",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="gray25",bg="magenta").pack()

Label(root,text="gray50",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="gray50",bg="orange").pack()

Label(root,text="gray75",font="verdana",fg="blue").pack()
Button(root,relief= RAISED,bitmap="gray75",bg="white").pack()


OUTPUT

Output of bitmap in python (itvoyagers.in)
Output of bitmap in python (itvoyagers.in)

Cursors

cursors are used to display mouse pointer on the screen

There are following type of cursors available −

“arrow”
“circle”
“clock”
“cross”
“dotbox”
“exchange”
“fleur”
“heart”
“man”
“mouse”
“pirate”
“plus”
“shuttle”
“sizing”
“spider”
“spraycan”
“star”
“target”
“tcross”
“trek”
“watch”

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date : 9th April 2020

Description : Program to show use of cursor attribute

"""
from tkinter import *
root=Tk()
Button(root,text="big circle",cursor="circle",relief=RAISED,fg="blue").pack()
Button(root,text="plus",cursor="plus",relief=RAISED,fg="blue").pack()
Button(root,text="cross",cursor="cross",relief=RAISED,fg="blue").pack()
Button(root,text="heart",cursor="heart",relief=RAISED,fg="blue").pack()
Button(root,text="watch",cursor="watch",relief=RAISED,fg="blue").pack()
Button(root,text="mouse",cursor="mouse",relief=RAISED,fg="blue").pack()
Button(root,text="spider",cursor="spider",relief=RAISED,fg="blue").pack()
Button(root,text="shuttle",cursor="shuttle",relief=RAISED,fg="blue").pack()
Button(root,text="star",cursor="star",relief=RAISED,fg="blue").pack()
Button(root,text="pirate",cursor="pirate",relief=RAISED,fg="blue").pack()
Button(root,text="arrow",cursor="arrow",relief=RAISED,fg="blue").pack()
Button(root,text="target",cursor="target",relief=RAISED,fg="blue").pack()
Button(root,text="tcross",cursor="tcross",relief=RAISED,fg="blue").pack()
Button(root,text="dotbox",cursor="dotbox",relief=RAISED,fg="blue").pack()
Button(root,text="fleur",cursor="fleur",relief=RAISED,fg="blue").pack()
Button(root,text="spraycan",cursor="spraycan",relief=RAISED,fg="blue").pack()
Button(root,text="trek",cursor="trek",relief=RAISED,fg="blue").pack()
Button(root,text="exchange",cursor="exchange",relief=RAISED,fg="blue").pack()
Button(root,text="sizing",cursor="sizing",relief=RAISED,fg="blue").pack()
Button(root,text="man",cursor="man",relief=RAISED,fg="blue").pack()
Button(root,text="clock",cursor="clock",relief=RAISED,fg="blue").pack()


OUTPUT

Output of Cursor in Python (itvoyagers.in)
Output of Cursor in Python (itvoyagers.in)

You can also check following posts to know more about tkinter and GUI

Leave a Comment