Search code examples
pythontkintertkinter-canvas

Tkinter canvas does not display entire image, small part is cropped off


I was trying to make a very basic app to do some work with images using Python and Tkinter and noticed 2 pixels are always "cropped off" the top and left of every image I display inside the canvas. My setup is Python 3.85 (AMD64) running on Windows 10.

import os, sys
import tkinter as tk

os.chdir(os.path.dirname(os.path.abspath(__file__)))

root = tk.Tk()
root.title("Test")
cv_wid, cv_hgt = 500, 300

cv = tk.Canvas(root, width=cv_wid, height=cv_hgt)
im_filename = "graphic_test.gif"
im = tk.PhotoImage(file=im_filename)
cv.create_image(0, 0, image=im, anchor=tk.NW)
cv.pack(side=tk.LEFT)
tk.mainloop()

graphic_test.gif file

Screenshot

20x Zoom of the screenshot showing image missing top 2 rows of pixels


Solution

  • Set the border of the canvas to 0 to avoid this issue. You can do so by setting the attributes borderwidth and highlightthickness to 0 as follows:

    cv = tk.Canvas(root, width=cv_wid, height=cv_hgt, borderwidth=0,highlightthickness=0)
    

    Result:

    enter image description here