如何使Tkinter画布背景透明? [英] How to make a tkinter canvas background transparent?

查看:1114
本文介绍了如何使Tkinter画布背景透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个国际象棋程序,我希望能够拖动这些棋子。为此,我将作品的图像放在 Canvas 上,以便可以拖动它(我也可以使用 Label )。但是,当我拖动该作品时,有一个白色正方形围绕着作品的图像。

I am making a chess program and I want to be able to drag the pieces. In order to do this, I put the image of the piece on a Canvas so it can be dragged (I can also use a Label if I want). However, when I drag the piece there is a white square that surrounds the image of the piece.

当我研究问题时,很多人给出了解决方案:

When I researched the problem, many people gave this solution:

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

这导致背景透明,但是看不到棋盘,而是GUI后面的程序

This caused the background to be transparent but it was not the chessboard that was visible, it was the program behind the GUI

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

Is there any way I can have the background be transparent and show the chessboard behind rather than the program behind the tkinter window?

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

Note: I do not mind using any other widget (e.g. a Label) but they must use modules that come default with Python (so no PIL) as this program needs to be used in an environment where I cannot download other modules.

推荐答案


问题:如何使Tkinter画布背景透明? p>

Question: How to make a tkinter canvas background transparent?

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

The only possible config(... option, to set the background to nothing


c.config(bg='')

结果为: _tkinter.TclError:未知颜色名称






要获得以下结果:


To get this result:

您必须将棋盘和数字放在同一个 .Canvas(... 内。

you have to hold the chess board and figures within the same .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

Tested with Python: 3.5 - TkVersion: 8.6

这篇关于如何使Tkinter画布背景透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆