bembry.org
Home / Technology / Python / Notes

Tkinter Lesson 6 Notes: Images in Tkinter

  • Two types of images in Tkinter:
    • Bitmaps
    • PhotoImages (gif, pgm, ppm)
  • Adding a Bitmap
    • bitmap = ""
      This widget option adds a bitmap
    • The option is available for: Button(), Checkbutton(), Label(), Menubutton(), Radiobutton()
    • bitmap = "" can take one of two values:
      • A built-in bitmap name
      • An "@" followed by the path to a bitmap
      • Label(root, bitmap="@/path/bitmapname")
    • Note that a true "bitmap" is not just a "bmp" file. (in other words, I haven't figured out how to use anything except the built-in bitmaps)
  • Built-in Bitmap Names
    • errorerror
    • hourglasshourglass
    • info info
    • questhead questhead
    • question question
    • warning warning
    • gray12, gray25, gray50, gray75
  • Bitmap Example
    Bitmap Exmaple from Tkinter import *
    root = Tk()
    root.title("Bitmap")

    Label(root, bitmap = "warning").grid(row=0, column=0)
    Label(root, text = "Beware of Bitmaps.").grid(row=0, column=1)
  • Images
    • The image = "" option will place an image in a widget
    • Tkinter only supports .gif, .pgm, and .ppm image formats
    • An image must be imported with PhotoImage before it can be used
    • my_picture = PhotoImage(file="pic.gif")
    • Label(root, image = my_picture)
  • Image Example
    from Tkinter import *
    root = Tk()
    root.title("Images")

    picture = PhotoImage(file="K:\\python\\images\\amgothic.gif")
    Label(root, image=picture).grid()

    Label(root, text = "American Gothic by Grant Wood").grid()
    Image Example
  • PhotoImage Methods
    • pic.subsample(x, y)
      Shrinks the image by using only every xth pixel. y is optional. Will flip image if given a negative number.
    • pic.zoom(x, y)
      Zoom in / enlarge image by a factor of x. y is optional.
    • pic.height()
      Returns the height of the picture in pixels
    • pic.width()
      Returns the width of the picture in pixels
  • PhotoImage Example
    from Tkinter import *
    root = Tk()
    root.title("Images")

    picture = PhotoImage(file="K:\\python\\images\\amgothic.gif")
    Label(root, image=picture).grid(row =0, column = 0)

    Label(root, text = "American Gothic by Grant Wood").grid(row = 1, column = 0)

    altpic = picture.subsample(3)
    Label(root, image=altpic).grid(row=0, column = 1, sticky = S)

    Label(root, text = "Smaller Image").grid(row=1, column = 1)
    PhotoImage Example
  • Customized Cursors
    • The cursor = "" widget option changes the look of the cursor when it is placed over a widget.
    • The cursor option can be used with any widget, including the root window.
    • The cursor option must be set to an existing Tkinter cursor choice.
  • Cursor Choices
    X_cursor
    arrow
    based_arrow_down
    based_arrow_up
    boat
    bogosity
    bottom_left_corner
    bottom_right_corner
    bottom_side
    bottom_tee
    box_spiral
    center_ptr
    circle
    clock
    coffee_mug
    cross
    cross_reverse
    crosshair
    diamond_cross
    dot
    dotbox
    double_arrow
    draft_large
    draft_small
    draped_box
    exchange
    fleur
    gobbler
    gumby
    hand1
    hand2
    heart
    icon
    iron_cross
    left_ptr
    left_side
    left_tee
    leftbutton
    ll_angle
    lr_angle
    man
    middlebutton
    mouse
    pencil
    pirate
    plus
    question_arrow
    right_ptr
    right_side
    right_tee
    rightbutton
    rtl_logo
    sailboat
    sb_down_arrow
    sb_h_double_arrow
    sb_left_arrow
    sb_right_arrow
    sb_up_arrow
    sb_v_double_arrow
    shuttle
    sizing
    spider
    spraycan
    star
    target
    tcross
    top_left_arrow
    top_left_corner
    top_right_corner
    top_side
    top_tee
    trek
    ul_angle
    umbrella
    ur_angle
    watch
    xterm
  • Cursor Example
    Cursor Example from Tkinter import *
    root = Tk()
    root.configure(cursor="hand1")

    cursor_list = ["umbrella", "trek", "spider", "spraycan", "sailboat", "gumby", "gobbler", "heart", "pirate", "man"]

    for cursor in cursor_list:
        Button(root, text = cursor, cursor = cursor).grid(sticky = EW)
Restricted access