Python Forum
[Tkinter] Transparent Canvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Transparent Canvas
#8
(Sep-19-2022, 06:39 PM)deanhystad Wrote: I cannot find a way to set the canvas background color to transparent. You could inherit the window's background. That will not solve the problem of drawing a canvas on a complex background, but it will work for your example.
import tkinter as tk
 
window = tk.Tk()
window.title("Drawing Shapes on Button Press")
window.geometry("300x300")
window.configure(background="green")
 
def circle():
    c = tk.Canvas(window, width=100, height=100, background=window["bg"], bd=0, highlightthickness=0)
    c.pack()
    c.create_oval(0, 0, 99, 99)
 
tk.Button(window, text="Circle", command=circle).pack()
 
window.mainloop()
For drawing on a complex background my suggestion is make a big canvas and draw everything on the canvas, including things like buttons and labels if need be.
import tkinter as tk
from random import randint

def circle():
    x = randint(0, 299)
    y = randint(0, 299)
    diameter = randint(10, 100)
    canvas.create_oval(x, y, x + diameter, y + diameter)


def rectangle():
    x = randint(0, 299)
    y = randint(0, 299)
    canvas.create_rectangle(x, y, x + randint(10, 100), y + randint(10, 100))


def text():
    x = randint(0, 299)
    y = randint(0, 299)
    canvas.create_text(x, y, text=f"x={x}, y={y}")


window = tk.Tk()
window.title("Drawing Shapes on Button Press")
window.geometry("300x300")
canvas = tk.Canvas(window, bg="green")
canvas.pack(expand=True, fill=tk.BOTH)
tk.Button(canvas, text="Circle", command=circle).pack(pady=10)
tk.Button(canvas, text="Rectangle", command=rectangle).pack()
tk.Button(canvas, text="Text", command=text).pack(pady=10)

window.mainloop()

Hi,

Think this is what Im gonna try. The idea is if i had an image in the background i can still see the image with the circle on top. This looks like a possible solution.
"Only Boring People Get Bored"
Reply


Messages In This Thread
Transparent Canvas - by finndude - Sep-18-2022, 02:59 PM
RE: Transparent Canvas - by woooee - Sep-18-2022, 05:16 PM
RE: Transparent Canvas - by deanhystad - Sep-19-2022, 06:39 PM
RE: Transparent Canvas - by finndude - Sep-22-2022, 10:02 AM
RE: Transparent Canvas - by Larz60+ - Sep-20-2022, 11:39 AM
RE: Transparent Canvas - by deanhystad - Sep-20-2022, 01:25 PM
RE: Transparent Canvas - by Larz60+ - Sep-20-2022, 06:23 PM
RE: Transparent Canvas - by deanhystad - Sep-20-2022, 08:36 PM
RE: Transparent Canvas - by joe_momma - Oct-02-2022, 01:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Transparent window background, but not text on it muzicman0 8 7,964 Feb-13-2025, 06:16 AM
Last Post: elonnmusk
  [Tkinter] COMPLEX! Transparent Canvas Linux AceScottie 7 4,846 Jun-21-2023, 06:44 PM
Last Post: AceScottie
  win32gui not transparent a color, just transparent all the wwindow DQT 0 2,003 Jul-29-2022, 01:40 PM
Last Post: DQT
  Make Label Text background (default color) transparent using tkinter in python barry76 1 27,952 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 26,709 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] How can we make the background transparent ? MrDrumX_ 1 12,400 Apr-27-2019, 09:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020