0

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 ...

1 Answers1

-1

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

Owl Surojit
  • 173
  • 1
  • 10
  • I don't think setting the border width to a negative number will have an effect? Have you tried this? When I do it, the overall size of the widget doesn't change between a value of zero and any negative value. – Bryan Oakley Aug 01 '21 at 23:47
  • 1
    `l1.image = bg1` will not update the image of the label like `Label(..., image=bg1)`, it just uses an attribute `image` to save the reference of the image. – acw1668 Aug 02 '21 at 00:40
  • 1
    You say “it worked”. What does that mean? What does a negative border look like? Are you certain you got something different with a -2 borderwidth and a 0 borderwidth? – Bryan Oakley Aug 02 '21 at 03:03
  • Yes you're right, borderwidth=0 works perfectly fine, I just edited the post. I had read somewhere that you would need bd=-2 to cancel out the default border, but that does of course not make a lot of sense... It works just fine, because negative borders just get treated as borders with a width of 0, but bd=0 is certainly more elegant. Thanks for the comment – Owl Surojit Aug 02 '21 at 15:35
  • Please, after adding a widget, the lines reappear. I also need to insert some widgets inside them. @OwlSurojit –  Aug 02 '21 at 18:34
  • @ShadowKurgansk I couldn't reproduce your problem, however try setting expand to 0 in all the pack() calls: `l1.pack(fill='both', expand=0)`. This will remove the effect that the Labels fill any exceeding space, when the parent Widget (in this case root) gets resized, thus making them "stick together". For adding widgets inside the Labels you need to be a little more specific... – Owl Surojit Aug 03 '21 at 16:37