2

I am trying to make it so that I have a canvas on tkinter for a turtle. I want the background of the canvas to be transparent. I use a mac. How would I do that? Here is my code:

import tkinter as tk
import string as s
import turtle as tur

root = tk.Tk()
root.title('Test')
root.geometry('1010x1010+10+10')

turtleCa = tk.Canvas(root, width=375, height=900)
turtleCa.place(x=0, y=0)
t = tur.RawTurtle(turtleCa)
t.speed(0)
t.hideturtle()

prog = 0


def hang():
    global prog
    global t
    if prog == 0:
        t.penup()
        t.goto(125, -20)
        t.pendown()
        t.goto(125, 300)
        t.goto(0, 300)
        t.goto(0, 275)
    elif prog == 1:
        t.left(180)
        t.circle(35)
    elif prog == 2:
        t.penup()
        t.goto(0, 205)
        t.pendown()
        t.goto(0, 185)
    elif prog == 3:
        t.goto(-100, 110)
    elif prog == 4:
        t.penup()
        t.goto(0, 185)
        t.pendown()
        t.goto(100, 110)
    elif prog == 5:
        t.penup()
        t.goto(0, 185)
        t.pendown()
        t.goto(0, 70)
    elif prog == 6:
        t.goto(-100, -5)
    elif prog == 7:
        t.penup()
        t.goto(0, 70)
        t.pendown()
        t.goto(100, -5)
    prog = prog + 1

for i in range(8):
    hang()

root.mainloop()
tur.mainloop()

I am trying to make the turtle Canvas (turtleCa)'s background be transparent such that the root behind it will still be visible. Thanks!

smlee
  • 131
  • 1
  • 9
  • @acw1668: Oops. Perhaps a duplicate of [How to make a tkinter canvas background transparent?](https://stackoverflow.com/questions/53021603/how-to-make-a-tkinter-canvas-background-transparent) – martineau May 30 '22 at 02:35
  • @martineau No the linked question has a misleading title. The answer to the linked question is how to draw *a transparent image on top of a image inside a canvas*. Actually OP of the linked question is doing what OP of this question wants. – acw1668 May 30 '22 at 02:59
  • Does anyone have any answers? – smlee May 30 '22 at 04:40
  • You can try calling `root.attributes('-transparent', 1)` and set the background color of the turtle canvas to "systemTransparent". See [tkdoc](https://tcl.tk/man/tcl8.6/TkCmd/wm.htm#M18) for details. – acw1668 May 30 '22 at 06:29
  • @acw1668 That didn't work for me... do you or anyone else have any ideas? – smlee May 30 '22 at 21:17
  • See this [answer](https://stackoverflow.com/a/44296157/5317403). It says that it works on Mac platform (I don't have a Mac, so I cannot test it). Did you change the background color of the turtle canvas to "systemTransparent"? – acw1668 May 31 '22 at 01:04
  • @acw1668 I tried that. It makes the area of the root behind the canvas transparent as well. What I am looking for is something so that only the canvas is transparent, not the root behind it (i.e. I should be able to see a label on the root if it is behind the canvas). – smlee May 31 '22 at 04:58
  • No you cannot because tkinter widget does not support transparent background. – acw1668 May 31 '22 at 05:34
  • @acw1668 Are you 100% sure its impossible? (just checking) – smlee May 31 '22 at 05:47
  • @smlee it is possible but i dont know if it will work on mac. I have posted an answer if you would want to see. – Dodu Jun 02 '22 at 12:54

1 Answers1

0

Instead of using root.attributes('-transparent', 1) you can use root.wm_attributes('-transparentcolor', 'red')

Here is the code

from tkinter import *
 
root = Tk()
root.geometry("400x400")
root.wm_attributes('-transparentcolor', 'red') #use a color you wont use in your project i am using red as an example
 
def transparent():
    height = 300
    width = 300

    c = Canvas(root, width = width, height = height, bg = 'red') #bg is 'red' so it is invisible
    c.pack(side="top", anchor = 'c', expand=True)

transparent()
root.mainloop()

How it looks like:

enter image description here

Dodu
  • 109
  • 2
  • 8