Tkinter canvas 不显示整个图像,小部分被裁掉

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

我试图制作一个非常基本的应用程序来使用 Python 和 Tkinter 对图像进行一些处理,并注意到我在 [= =26=]。我的设置是 Python 3.85 (AMD64) 运行 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

将 canvas 的边框设置为 0 以避免此问题。您可以通过将属性 borderwidthhighlightthickness 设置为 0 来实现,如下所示:

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

结果: