我试图重建一张被分成6个部分的图片,将这些部分整齐地添加到垂直排序的6个标签上。图片的大小与标签的大小完全相同,但添加标签后,要么标签变得越来越小,要么图片的大小没有达到预期的大小,因此图片部分无法合并。为了证明这一点,我用自己制作的函数检查了添加图像前后标签的大小。
带有图片的标签代码,它们的大小和结果:
Lwidth = 512 #root.winfo_width()
Lheight = 115 #root.winfo_height() // 6
img1=Image.open('row-1.png')
bg1=ImageTk.PhotoImage(img1)
l1 = Label(root, image=bg1)
l1.pack(fill='both', expand=True)
l1.image = bg1
l1.update()
img2=Image.open('row-2.png')
bg2=ImageTk.PhotoImage(img2)
l2 = Label(root, image=bg2)
l2.pack(fill='both', expand=True)
l2.image = bg2
l2.update()
img3=Image.open('row-3.png')
bg3=ImageTk.PhotoImage(img3)
l3 = Label(root, image=bg3)
l3.pack(fill='both', expand=True)
l3.image = bg3
l3.update()
img4=Image.open('row-4.png')
bg4=ImageTk.PhotoImage(img4)
l4 = Label(root, image=bg4)
l4.pack(fill='both', expand=True)
l4.image = bg4
l4.update()
img5=Image.open('row-5.png')
bg5=ImageTk.PhotoImage(img5)
l5 = Label(root, image=bg5)
l5.pack(fill='both', expand=True)
l5.image = bg5
l5.update()
img6=Image.open('row-6.png')
bg6=ImageTk.PhotoImage(img6)
l6 = Label(root, image=bg6)
l6.pack(fill='both', expand=True)
l6.image = bg6
l6.update()
root.update()

结果是:

这是我从没有图像的代码中得到的函数:

我完全确定问题不在图像上,我尝试了许多不同的方法来确保它们被正确划分,而不会丢失一些部分……
发布于 2021-08-01 23:22:02
问题是tkinter标签默认情况下有一个2px宽的白色边框;这就是为什么那些白线出现在图像之间。
要更改这一点,只需将bd=0 (或borderwidth=0)添加到所有标签。像这样:l1 = Label(root, image=bg1, bd=0)
这是我的结果(忽略右边的白线,这只是我糟糕的剪切工具技能...):

https://stackoverflow.com/questions/68614965
复制相似问题