Python 不绘制 tkinter 标签图像,除非它后面有消息小部件
Python tkinter label image not drawn unless a message widget is behind it
我有一个透明的 tkinter 消息小部件和一个标签,一个包含文本,另一个包含图像,图像仅绘制在文本标签后面的部分。 1
我在 windows 7 上使用 python tkinter 在透明 window 上绘制桌面上的标签,所以你在背景中看到的是我的常规windows 桌面,这可能与它有关
这是代码片段,但我认为还有很多不相关的代码
x = Tk()
label = tkinter.Message(x, textvariable=text, font=('Terminal','10'), fg='white', bg='green', width=800, anchor='n')
label.master.overrideredirect(True)
label.master.geometry('+30+30')
label.master.lift()
label.master.wm_attributes('-transparentcolor', 'green')
label.pack()
然后这是一个调用显示绘图的函数内部
new_plot = Label(x, image=plot_image, fg='white', bg='green', anchor='n', width=640, height=480)
new_plot.image = plot_image
new_plot.master.wm_attributes('-transparentcolor', 'green')
new_plot.place(x = 20, y = 30, width=640, height=480)
new_plot.update()
然后在最后
x.mainloop()
我希望这就是与问题相关的全部内容
我怀疑这与 tkinter window 默认大小为 1x1 像素有关。当您使用 place
时,tkinter 不会扩展 window 以适应其内容。
它与消息框一起工作的原因是因为您使用 pack
将消息框添加到根 window,这 将 导致 window 成长。
简单的解决方案是使用 new_plot.pack(...)
而不是 new_plot.place(...)
。
我有一个透明的 tkinter 消息小部件和一个标签,一个包含文本,另一个包含图像,图像仅绘制在文本标签后面的部分。 1
我在 windows 7 上使用 python tkinter 在透明 window 上绘制桌面上的标签,所以你在背景中看到的是我的常规windows 桌面,这可能与它有关
这是代码片段,但我认为还有很多不相关的代码
x = Tk()
label = tkinter.Message(x, textvariable=text, font=('Terminal','10'), fg='white', bg='green', width=800, anchor='n')
label.master.overrideredirect(True)
label.master.geometry('+30+30')
label.master.lift()
label.master.wm_attributes('-transparentcolor', 'green')
label.pack()
然后这是一个调用显示绘图的函数内部
new_plot = Label(x, image=plot_image, fg='white', bg='green', anchor='n', width=640, height=480)
new_plot.image = plot_image
new_plot.master.wm_attributes('-transparentcolor', 'green')
new_plot.place(x = 20, y = 30, width=640, height=480)
new_plot.update()
然后在最后
x.mainloop()
我希望这就是与问题相关的全部内容
我怀疑这与 tkinter window 默认大小为 1x1 像素有关。当您使用 place
时,tkinter 不会扩展 window 以适应其内容。
它与消息框一起工作的原因是因为您使用 pack
将消息框添加到根 window,这 将 导致 window 成长。
简单的解决方案是使用 new_plot.pack(...)
而不是 new_plot.place(...)
。