Skip to content
Advertisement

Label’s size changing after inserting an image

I am trying to reconstruct a picture that is divided into 6 parts, by adding the parts neatly to the 6 labels ordered vertically. The pictures have the exact size with labels but still after adding them either labels are getting smaller or either images aren’t getting the size they are meant to have therefore the picture parts aren’t uniting. To prove this I’ve checked the size of labels before and after adding the images with a function I’ve made.

Code of labels with images, their sizes and the result:

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()

enter image description here

The result:

enter image description here

And this is what I get from the function I’ve made from the code without images:

enter image description here

I’m totally sure that the problem is not with images, I tried many different ways on them to be sure that they are divided correctly, without losing some parts …

Advertisement

Answer

The issue is that tkinter Labels have a 2px wide white border by default; that’s why those white lines appear between the Images.

To change this, simply add bd=0 (or borderwidth=0) to all the Labels. Like this: l1 = Label(root, image=bg1, bd=0)

Here is my result (ignore the white lines on the right, that’s just my poor Snipping Tool skills…):

Complete picture

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement