【问题标题】:How do you make an image be the only visible part of a label?如何使图像成为标签的唯一可见部分?
【发布时间】:2013-08-27 16:45:20
【问题描述】:

下面是一些示例代码:

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root['background'] = 'black'

# this image here is just a placeholder. For the real application I will be using an image that is not a solid color.
image = tk.PhotoImage(width=1,height=1)
image.put(data='red', to=(0,0))
image = image.zoom(32, 32)

label = ttk.Label(root, image=image)
label.grid()

label = tk.Label(root, image=image)
label.grid(row=1,column=1)

root.mainloop()

此代码会将图像放入Label 小部件中,但对我而言,问题是图像周围有边框,如此屏幕截图所示:

如何使图像成为标签的唯一可见部分(即,您只看到没有边框/填充的图像)?

【问题讨论】:

  • 边界。有边界。

标签: python user-interface tkinter tcl tk


【解决方案1】:

更改padxpady 配置参数没有帮助。

对于 tk.Label,解决方案是更改 borderwidth:

tk.Label(root, image=image, borderwidth=0)

对于 ttk.Label,您必须创建自定义样式并将 ttk.Label() 的样式设置为自定义样式。我不确定您将如何为 ttk.Label 制作自定义样式。

【讨论】:

  • 标签有一个highlightthickness 属性,它也需要设置为零。如果您想删除所有边框并显示图像,则使用 ttk 标签小部件是没有意义的。
  • @BryanOakley re - “使用 ttk 标签小部件没有意义”:你这么说是因为如果你只想显示图像......那么你不需要任何主题/styling ttk.Label 的用途是什么?
  • 是的。 ttk 小部件添加的唯一值是主题和装饰,在这种情况下将被关闭。
猜你喜欢
  • 2014-09-02
  • 2021-08-24
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多