pythonpython-3.xcanvastkintertransparent

How to make a tkinter canvas background transparent?


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.

enter image description here

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

enter image description here.

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.


Solution

  • Question: How to make a tkinter canvas background transparent?

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

    c.config(bg='')
    

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


    To get this result:

    enter image description here

    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