11

I have the following code that uses Tkinter to create a window and draw shapes on a canvas inside it.

from Tkinter import *

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.parent = parent 
        self.initUI()

    def initUI(self):

        self.parent.title("Colors")
        self.pack(fill=BOTH, expand=1)

        canvas = Canvas(self)

        canvas.create_oval(10, 10, 80, 80, outline="red", fill="green", width=2)
        canvas.create_oval(110, 10, 210, 80, outline="#f11", fill="#1f1", width=2)
        canvas.create_rectangle(20, 50, 300, 100, outline="black", fill="red", width=2)

        canvas.pack(fill=BOTH, expand=1)


if __name__ == '__main__':
    root = Tk()
    ex = Example(root)
    root.geometry("400x400+100+100") # WIDTHxHEIGHT+X+Y
    root.mainloop()

The rectangle sits on top of the two ovals. Is there any way that I can make the rectangle partially transparent (so the ovals' outlines can be seen)?

Neal Ehardt
  • 10,334
  • 9
  • 41
  • 51
  • 1
    Please see other Q&A: https://stackoverflow.com/questions/54637795/how-to-make-a-tkinter-canvas-rectangle-transparent – WinEunuuchs2Unix Dec 01 '19 at 22:52
  • Very clever! I wonder how this can be generalized to other shapes (transparent ovals, polygons, lines, text, etc). It is surprising that Tkinter is able to draw transparent images but other primitives aren't supported. – Neal Ehardt Dec 03 '19 at 01:54
  • I posted an answer and pointed out how it isn't "true alpha". – WinEunuuchs2Unix Dec 03 '19 at 02:01

4 Answers4

16

I am not completely sure, but I think it is not possible to set a RGBA color as a fill color of a canvas item. However, you can give a try to the stipple option:

canvas.create_rectangle(20, 50, 300, 100, outline="black", fill="red", width=2, stipple="gray50")
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
7

You cannot change the alpha of items on a canvas.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 9
    How do you _know_ this? Where on earth is it documented? Oh the frustration! – Matthew Walker Oct 02 '16 at 21:07
  • 1
    @MatthewWalker: I know it because no documentation says you can. The official tk documentation is very precise and complete, and nowhere does it mention alpha channels. – Bryan Oakley Oct 02 '16 at 23:35
  • 1
    Thanks for that clarification. That would make providing a source pretty difficult ;o) Given that you can't do it natively in Tkinter, is there an alternative? I was considering using Cairo to render an image that could be displayed in a Canvas. Any better ideas? – Matthew Walker Oct 03 '16 at 00:58
  • @MatthewWalker, any success with that? – Eshita Shukla Aug 10 '18 at 11:17
  • It can be done according to other answers: https://stackoverflow.com/questions/54637795/how-to-make-a-tkinter-canvas-rectangle-transparent – WinEunuuchs2Unix Dec 01 '19 at 22:51
  • @WinEunuuchs2Unix: no, that other answer shows how to simulate it using an image. You can't change the alpha of anything else on the canvas except images. – Bryan Oakley Dec 02 '19 at 00:24
  • I've been using `stipple` tonight and it seems to work ok with canvas rectangle. – WinEunuuchs2Unix Dec 02 '19 at 01:18
  • @WinEunuuchs2Unix, yes, using a stipple will simulate it, but it doesn’t change the alpha channel. – Bryan Oakley Dec 02 '19 at 03:39
5

An idea is make a rectangle png image with semitransparent colors. Then use create_image instead of create_rectangle.

godlovesdavid
  • 169
  • 2
  • 5
0

You can't change the alpha but you can get simulated transparency with the stipple option in regular tkinter. For example using:

mycanvas.create_rectangle(X1, Y1, X2, Y2, fill=color, stipple="gray50")

yields:

resize1.png

WinEunuuchs2Unix
  • 1,801
  • 1
  • 17
  • 34