- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
根据几篇 SO 帖子的建议,我创建了一个 Label
到 .place()
一张 BMP
图片作为窗口的背景。不幸的是,它不起作用,因为图片没有出现。
import Tkinter as tk
root = tk.Tk()
root.geometry("200x200")
# generic background
root.tk_setPalette(background='black', foreground='white')
# the background I want to have (but it does not appear)
background_image = tk.PhotoImage("background.bmp")
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# a label with a long text which will be shortened afterwards
mylabel = tk.Label(root, text="a long text", font=("Arial", 30))
mylabel.grid(column=0, row=0)
# the text is shortened to make sure the background adapts
mylabel.configure(text="short")
root.mainloop()
上面的
background.bmp
与窗口大小相同(200x200):
运行代码时我得到:
而不是(减去框架)
我在代码中添加了标签中显示的文本更改,以确保背景适应(= 如果文本更改,则在下面恢复原始背景)
最佳答案
来自帮助(Tk.PhotoImage)
:
Widget which can display colored images in GIF, PPM/PGM format.
因此,您需要将图像保存为 GIF 或可移植位图 (PPM/PGM)。
接下来,我认为您将 PIL
的 ImageTk.PhotoImage
类与 Tkinter
的内置 混合在一起tk.PhotoImage
类。 tk.PhotoImage
类需要在其构造函数中分配一个 name
参数:
| __init__(self, name=None, cnf={}, master=None, **kw)
| Create an image with NAME.
|
| Valid resource names: data, format, file, gamma, height, palette,
| width.
所以你只需要指定文件,如下所示:
background_image = tk.PhotoImage(file="background.gif")
但是,稍后您会遇到另一个问题,因为您会发现 Label
小部件具有不透明的背景颜色,因此它不会像您预期的输出那样显示。您可以通过使用 Canvas
小部件来保存背景并使用它的 create_text()
方法在背景上制作文本来克服这个问题。
编辑:这是一个使用 Canvas
制作背景的简单示例,它将保留文本的透明度:
from Tkinter import *
root = Tk()
canvas = Canvas(root, width=200, height=200)
canvas.pack(fill=BOTH, expand=1)
bg = PhotoImage(file='background.gif')
canvas.create_image(0, 0, image=bg)
canvas.create_text(50, 50, text='Short')
root.mainloop()
关于python - 使用 Label 和 .place() 时,为什么 tkinter 根背景不设置为 BMP 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23735821/
在whatsapp中,如果消息很短,文本和时间在同一行。如果消息很长,时间在右下角 - 上面的文字。 我如何在 Ios 中使用 Storyboard 实现此目的 最佳答案 尝试使用类似这样的方法来定义
我有这段代码: label.control-label{ font-weight: bold; } label.control-label::after{ content: ":";
尊敬的社区成员, 我想将测试中的文本放在 div 的中心。代码如下所示: Testing everything: 现在,如果我尝试以下代码部分: Testing everything: 它不会在
我有一个 DIV 元素,它有一个 并在其中输入文本框。 基本上,我在 DIV 元素上启用了 jQuery .resizable(),但是当您使 DIV 元素小于当前大小时,文本框会被推到新的一行。 我
请考虑以下标记。 This is a label 对我来说,这个标记是在我的自定义工具提示控件之后生成的。我在 IE 上的 JAWS 上看到的问题是它只读取“标题,而不是标签”,但是对于其他屏幕阅读
我正在按照文档使用 ionic 2 构建应用程序。我已经实现了一个带有 fab-list 的 fab 按钮。我试图在包含按钮旁边放置一个描述性标签。开箱即用的 ionic 2 似乎无法在 float
通常我使用标签标签来指向这样的输入标签 First Name: 现在我有了这个 First Name: 由于我以前没有穿过这样的东西,是否可以为 label 添加 label 标签。当我应用 Ja
我有一个包含换行符(“\r”)的传入文本字符串。 当我输出它时: System.out.println(myString) , 回车被解释。 但是,当我将字符串设置为标签的内容时,它会忽略回车。 如何
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 1年前关闭。 Improve thi
在 Excel 2013 中,我使用单元格中的值标记散点图。我希望标签不重叠。我可以手动移动标签,但我创建了一个过滤器来自动创建新绘图,因此我希望标签冲突也能自动发生。 这可能吗?无需 VBA 的解决
在我的 Struts2 JSP 中,我想显示一个 id,所以我写道: A${id}B ( A 和 B 用于调试) 我希望它显示为 Id:A7B 但 HTML 中生成了以下内容:A7BId: 为什么标签
我想要一个带注释的 AST,所以我定义了那些递归数据结构 使用 Fix : data Term a = Abstraction Name a | Application a a | Var
这两种方法都没有记录,并且似乎没有达到我的预期。 mylabel.setFontScale(3f); 使明显文本变大 3 倍(我正在寻找的),但与 Align.center 一起使用时无法正确居中>.
ScrollView里面有两个Label(多边的),下面是TableView(其中行数可能不同) Label 和 TableView 的高度都没有设置。 所有 outlet 都对彼此上方和下方的缩进设
我很好奇是否有一种简单的方法可以使标签采用 CSS 样式属性的默认值。我的复选框采用了我的选项卡的属性,我只希望它们成为默认值。正如您将看到的,我更改了复选框的字体大小,使其小于选项卡。但是,我不想仅
asp:label 和 html label 有什么区别? 我知道第一个是在服务器上呈现的,所以基本上它会返回一个跨度选项卡,但它有什么用呢?在什么情况下需要使用 HTML 标记,在什么情况下需要使用
我需要从网站中提取所有城市名称。我在以前的项目中使用了 beautifulSoup 和 RE,但在这个网站上,城市名称是常规文本的一部分,没有特定的格式。我找到了满足我要求的地理包 ( https:/
您好,我正在尝试添加 到表格的每个单元格。我在这里使用 Material 表:https://material-table.com/#/docs/features/component-overridi
我想制作一个简单的 R 图,y 轴标签位于 y 轴刻度标签上方。我用下面的代码创建了我喜欢的东西。但是它需要对 at 进行一些摸索。图形参数。 问:有没有更简单的方法来做到这一点?有没有办法查询 y
我可以绘制以下 df 的标签使用 geom_text : df 1 8 var 2 426 -276 hours worked per week N
我是一名优秀的程序员,十分优秀!