首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >插入图像后标签的大小发生变化

插入图像后标签的大小发生变化
EN

Stack Overflow用户
提问于 2021-08-01 22:58:04
回答 1查看 51关注 0票数 0

我试图重建一张被分成6个部分的图片,将这些部分整齐地添加到垂直排序的6个标签上。图片的大小与标签的大小完全相同,但添加标签后,要么标签变得越来越小,要么图片的大小没有达到预期的大小,因此图片部分无法合并。为了证明这一点,我用自己制作的函数检查了添加图像前后标签的大小。

带有图片的标签代码,它们的大小和结果:

代码语言:javascript
运行
复制
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()

结果是:

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

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

EN

Stack Overflow用户

回答已采纳

发布于 2021-08-01 23:22:02

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

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

这是我的结果(忽略右边的白线,这只是我糟糕的剪切工具技能...):

票数 -1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68614965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档