Sep-22-2022, 10:02 AM
(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"