首页 > 解决方案 > 插入图像后标签的大小发生变化

问题描述

我正在尝试通过将这些部分整齐地添加到垂直排序的 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()

在此处输入图像描述

结果:

在此处输入图像描述

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

在此处输入图像描述

我完全确定问题不在于图像,我尝试了许多不同的方法来确保它们被正确分割,而不会丢失某些部分......

标签: pythonpython-3.xwindowstkintertkinter-label

解决方案


问题是 tkinter 标签默认有 2px 宽的白色边框;这就是为什么那些白线出现在图像之间的原因。

要更改这一点,只需将bd=0(或borderwidth=0)添加到所有标签。像这样:l1 = Label(root, image=bg1, bd=0)

这是我的结果(忽略右边的白线,那只是我可怜的 Snipping Tool 技能......):

全貌


推荐阅读