Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tkinter中没有背景的帧_Python_Python 3.x_Tkinter - Fatal编程技术网

Python tkinter中没有背景的帧

Python tkinter中没有背景的帧,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我有一个背景图片在Tkinter的画布。我想添加一个没有背景的框架,这样我就可以在窗口中排列元素,但仍然可以看到这些元素后面的背景。当我使用类似于下面代码的东西时,也就是说,没有指定背景颜色,我得到一个灰色背景的帧。我怎样才能把它变成没有背景的呢 import tkinter as tk from PIL import Image,ImageTk root=tk.Tk() root.geometry("800x560") bgImg=Image.open("da

我有一个背景图片在Tkinter的画布。我想添加一个没有背景的框架,这样我就可以在窗口中排列元素,但仍然可以看到这些元素后面的背景。当我使用类似于下面代码的东西时,也就是说,没有指定背景颜色,我得到一个灰色背景的帧。我怎样才能把它变成没有背景的呢

import tkinter as tk
from PIL import Image,ImageTk

root=tk.Tk()
root.geometry("800x560")

bgImg=Image.open("data/bg.png")
bgImg=ImageTk.PhotoImage(bgImg)
canvas=tk.Canvas(root,width=800,height=560)
canvas.pack(expand = False, fill = "both")

canvas.create_image(0, 0, image=bgImg, anchor="nw")

frame=tk.Frame(canvas,width=50,height=50)
frame.place(relx=0.5,rely=0.5,anchor="center")

root.mainloop()

可能存在*依赖于平台的选项,如。
(*在Lubuntu 18.04和python 3.6.9中,它们似乎不起作用,
所以我无法测试它们。)

一个跨平台(?)选项是使用画布。
然后在画布上绘制图像*,而不是使用小部件。
(*其alpha通道决定其透明像素)
这将正确绘制前景/背景元素,
&画布具有足够的功能来控制其上的元素
(但如果您想模拟完整的小部件,则需要进行一些工作)

Canvas提供了各种
create_xyz
方法,其中xyz:
圆弧
直线
矩形
位图
椭圆形
文本
图像
多边形


窗口
(可能*有平台相关选项,如。
(*在Lubuntu 18.04和python 3.6.9中,它们似乎不起作用,
所以我无法测试它们。)

一个跨平台(?)选项是使用画布。
然后在画布上绘制图像*,而不是使用小部件。
(*其alpha通道决定其透明像素)
这将正确绘制前景/背景元素,
&画布具有足够的功能来控制其上的元素
(但如果您想模拟完整的小部件,则需要进行一些工作)

Canvas提供了各种
create_xyz
方法,其中xyz:
圆弧
直线
矩形
位图
椭圆形
文本
图像
多边形


窗口
(tkinter小部件不支持透明背景。tkinter小部件不支持透明背景。
import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()

# Note:
# If the images overlap exactly (same position & extent),
# the bottom one will never? get any events.
# Maybe events can be propagated, not sure right now.

fgImg = tk.PhotoImage(master=root, file='media/fg.png')
fgImgId = canvas.create_image(
  80, 80, anchor=tk.NW, image=fgImg
)
canvas.tag_bind(
  fgImgId,
  '<ButtonRelease-1>',
  lambda e: canvas.tag_raise(fgImgId)
)

bgImg = tk.PhotoImage(master=root, file='media/bg.png')
bgImgId = canvas.create_image(
  0, 0, anchor=tk.NW, image=bgImg
)
canvas.tag_bind(
  bgImgId,
  '<ButtonRelease-1>',
  lambda e: canvas.tag_raise(bgImgId)
)

root.mainloop()