问题描述
我正在编写一个国际象棋程序,我希望能够拖动棋子.为此,我将作品的图像放在 Canvas
上,以便可以拖动它(如果需要,我也可以使用 Label
).但是,当我拖动该作品时,该作品的图像周围会出现一个白色方块.
当我研究这个问题时,很多人给出了这个解决方案:
drag_canvas = Canvas(self, height=80, width=80, bg="yellow")root.wm_attributes("-transparentcolor", "黄色")
这导致背景透明但不是棋盘可见,而是GUI背后的程序
.
有什么办法可以让背景透明并显示后面的棋盘而不是 tkinter 窗口后面的程序?
注意:我不介意使用任何其他小部件(例如 Label
),但它们必须使用 Python 默认的模块(因此没有 PIL),因为该程序需要在环境中使用我无法下载其他模块.
唯一可能的 config(...
选项,将背景设置为空
c.config(bg='')
结果为:_tkinter.TclError:未知颜色名称"
要得到这个结果:
你必须把棋盘和数字放在同一个 .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
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")
This caused the background to be transparent but it was not the chessboard that was visible, it was the program behind the GUI
.
Is there any way I can have the background be transparent and show the chessboard behind rather than the program behind the tkinter window?
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.
The only possible config(...
option, to set the background to nothing
To get this result:
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)
Tested with Python: 3.5 - TkVersion: 8.6
这篇关于如何使 tkinter 画布背景透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!