Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a tkinter canvas rectangle transparent?

I need to make my tkinter rectangles transparent. Does anyone know how to do that?

I have tried to specify alpha=".5", opacity=".5", and I have tried to add two more digits in the end of the color code: fill="#ff000066". None of these things seem to work, I can't find the right syntax.

import tkinter

root = tkinter.Tk()
canvas = tkinter.Canvas(root, width=800, height=600)
canvas.pack()

canvas.create_rectangle(50, 50, 100, 100, fill="#ff0000", alpha=0.5)

root.mainloop()

This code gives me this message: _tkinter.TclError: unknown option "-alpha", so this is obviously not the right way to do it.

like image 295
Jånny Avatar asked Feb 11 '19 19:02

Jånny


People also ask

Can you make a canvas transparent Tkinter?

Build A Paint Program With TKinter and Python If we want to create a transparent window in an application, then we should have to define the color in the attributes('-transparentcolor', 'color' ) method. By providing the color of the window and widget, it will make the window transparent.

How do you clear a python canvas?

To clear a Tkinter clear canvas, you can use the delete method. The delete method ensures you to avoid memory leaks and not end up creating thousands of objects. Note : Here the string "all" is a special tag that represents all items on the canvas.

Is there a transparent color in Tkinter?

A Tkinter widget in an application can be provided with Transparent background. The background property of any widget is controlled by the widget itself. However, to provide a transparent background to a particular widget, we have to use wm_attributes('transparentcolor', 'colorname') method.

How do I change the background color of a canvas in Python?

To change the background color of the Canvas widget, you can use the configure() method. Here, you can specify the background color of the Canvas widget which you want to change explicitly.


2 Answers

You can use transparent image to simulate the result. Use Pillow to create transparent image and then use canvas.create_image(...) to draw it. Below is a sample code:

from tkinter import *
from PIL import Image, ImageTk

root = Tk()

images = []  # to hold the newly created image

def create_rectangle(x1, y1, x2, y2, **kwargs):
    if 'alpha' in kwargs:
        alpha = int(kwargs.pop('alpha') * 255)
        fill = kwargs.pop('fill')
        fill = root.winfo_rgb(fill) + (alpha,)
        image = Image.new('RGBA', (x2-x1, y2-y1), fill)
        images.append(ImageTk.PhotoImage(image))
        canvas.create_image(x1, y1, image=images[-1], anchor='nw')
    canvas.create_rectangle(x1, y1, x2, y2, **kwargs)

canvas = Canvas(width=300, height=200)
canvas.pack()

create_rectangle(10, 10, 200, 100, fill='blue')
create_rectangle(50, 50, 250, 150, fill='green', alpha=.5)
create_rectangle(80, 80, 150, 120, fill='#800000', alpha=.8)

root.mainloop()

And the output:

enter image description here

like image 104
acw1668 Avatar answered Oct 16 '22 12:10

acw1668


In a canvas if you want some widget to be transparent, simply let the fill parameter be empty:

fill=""
like image 11
delonglée Avatar answered Oct 16 '22 10:10

delonglée