python - 如何使 tkinter Canvas 背景透明?

标签 python python-3.x canvas tkinter transparent

我正在制作一个国际象棋程序,我希望能够拖动棋子。为此,我将作品的图像放在 Canvas 上,以便可以拖动它(如果需要,我也可以使用 Label)。但是,当我拖动该作品时,该作品的图像周围会出现一个白色方 block 。

enter image description here

我研究这个问题的时候,很多人给出了这样的解决方案:

drag_canvas = Canvas(self, height=80, width=80, bg="yellow")
root.wm_attributes("-transparentcolor", "yellow")

这导致背景透明但不是棋盘可见,是GUI背后的程序

enter image description here .

有什么方法可以让背景透明并显示后面的棋盘而不是 tkinter 窗口后面的程序?

注意:我不介意使用任何其他小部件(例如 Label),但它们必须使用 Python 默认的模块(因此没有 PIL),因为此程序需要在环境中使用我无法下载其他模块的地方。

最佳答案

Question: How to make a tkinter canvas background transparent?

唯一可能的 config(... 选项,将背景设置为空

c.config(bg='')

results with: _tkinter.TclError: unknown color name ""


要得到这个结果:

enter image description here

您必须将棋盘和图形放在同一个 .Canvas(....

    self.canvas = Canvas(self, width=500, height=200, bd=0, highlightthickness=0)
    self.canvas.create_rectangle(245,50,345,150, fill='white')

    self.image = tk.PhotoImage(file='chess.png')
    self.image_id = self.canvas.create_image(50,50, image=self.image)

    self.canvas.move(self.image_id, 245, 100)

使用 Python 测试:3.5 - TkVersion:8.6

关于python - 如何使 tkinter Canvas 背景透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53021603/

相关文章:

python - 使用Python从以太坊区 block 链中提取信息

python - Shutil - DS_store 文件阻塞移动问题

python - python 中的跟踪函数

javascript - 去除 mousemove 背景图像的模糊效果

python - 在Python中使用OpenCV2去除接缝后,如何判断图像的大小?

Python 开始计算长度为 x 的数组,从第二项开始到第一项结束

Python 3 和 Gspread 导入错误

python - 重新排序列表,尽可能使所有先前数字的总和小于当前数字

python - 使用 python 编辑 outlook 分发列表

javascript - 如何让用户在单击 Canvas 时创建一个圆圈