0

I'm getting an uknown option - alpha error. I don't know why. I'm trying to create a transparent rectangle, and I need to set the alpha value to do this. I want it so that when one presses on the transparent rectangle, it acts as a button. Right now, only the outline of the rectangle acts as a button, I would like the whole rectangle with a transparent filling to act as a button.

class MainWindow(Tk):

   def __init__(self, *args, **kwargs):

        Tk.__init__(self, *args, **kwargs)
        container = Frame(self, width = 165, height = 165)
        container.grid(row = 0, columnspan = 4, rowspan = 
                          50,sticky=N+S+W+E, padx=55, pady=40)
        container.grid_rowconfigure(0, weight = 5)
        container.grid_columnconfigure(0, weight = 5)

        self.frames = {}
        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
        self.show_frame(StartPage)

   def show_frame(self,cont):
       frame = self.frames[cont]
       frame.tkraise()
       frame.grid(row = 0, column = 2, sticky = "nsew")

class StartPage(Frame,MainWindow):

  def create_rectangle(x1, y1, x2, y2, **kwargs):
       images = []
       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))
          self.canvas.create_image(x1, y1, image=images[-1], 
                                      anchor='nw')
       self.canvas.create_rectangle(x1, y1, x2, y2, **kwargs)

 def __init__(self,parent,controller):

      frame = Frame.__init__(self, parent, width = 165, height = 165)
      self.canvas = Canvas(self, width = 165, height = 165)
      self.canvas.configure(highlightthickness=0, borderwidth=0)
      self.canvas.grid(pady=(25,0)
      self.filenamesBCRed = [img for img in glob.glob(pathBCred)] 
      self.filenamesBCRed_sorted = 
                              natsort.natsorted(self.filenamesBCRed)
      self.iter = 1 #iteration number
      self.img_num = 0
      self.count = 0
      (x,y) = left_bottom_coordinates[self.box_index]
      self.rect = create_rectangle(x,y, x+80,y+80, outline = 'red', 
                  width = 1, fill ='gray', alpha = .5)
      self.canvas.tag_bind(self.rect, '<Button-1>', self.onButton)

I get an unknown option -alpha error. This is the full error message:

Traceback (most recent call last):
  File "Interactive3", line 302, in <module>
    root = MainWindow()
  File "Interactive3", line 31, in __init__
    frame = F(container, self)
  File "Interactive3", line 72, in __init__
    self.canvas.create_rectangle(50, 50, 250, 150, fill='green', alpha=.5)
  File "/anaconda3/lib/python3.6/tkinter/__init__.py", line 2498, in create_rectangle
    return self._create('rectangle', args, kw)
  File "/anaconda3/lib/python3.6/tkinter/__init__.py", line 2477, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: unknown option "-alpha"
ishika
  • 9
  • 2
  • You have defined a function of your own named `create_rectangle()` that takes an `alpha=` keyword parameter. You then proceed to call Tkinter's own `.create_rectangle()` method, somehow expecting it to also accept this parameter. – jasonharper Jul 22 '19 at 16:22
  • The code you posted doesn't even compile, there is no way it can raise a `TclError`. – Stop harming Monica Jul 22 '19 at 16:23
  • @jasonharper how do I call the create_rectangle() function I made? – ishika Jul 22 '19 at 16:25
  • It is not because undefined variables but because the broken indentation. Also I said "the code you posted". Whatever other code you are running may compile or not, I don't know because I can't see it. – Stop harming Monica Jul 22 '19 at 16:28
  • 1
    always put full error message (starting at word "Traceback") in question (as text, not screenshot). There are other useful information. – furas Jul 22 '19 at 23:01
  • You should just call `create_rectangle(..., alpha=.5)` not `self.canvas.create_rectangle(..., alpha=.5)`. Refer to my answer [`here`](https://stackoverflow.com/questions/54637795/how-to-make-a-tkinter-canvas-rectangle-transparent/54645103#54645103). – acw1668 Jul 23 '19 at 01:45
  • @acw1668 I get this error message when I put create_rectangle instead of self.canvas.create_Rectangle: name 'create_rectangle' is not defined – ishika Jul 23 '19 at 03:44
  • And that is why we need a [mcve] in order to tell exactly what is going on instead of guessing. – Stop harming Monica Jul 23 '19 at 06:46
  • @Goyo I reposted my code to give you a better idea of what's going on. Hopefully this is more helpful. – ishika Jul 23 '19 at 14:27
  • @acw1668 I reposted my code to give you a better idea of what's going on – ishika Jul 23 '19 at 14:27

2 Answers2

1

Since you declare create_rectangle(...) inside class StartPage, you need to add self as the first argument. Also images = [] should be changed to self.images = [] and move to class __init__(...) function. As you need to bind key to the transparent rectangle, you need to return the item ID as well:

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

Then you need to call the function as self.create_rectangle(...):

    def __init__(self, parent, controller):
        ...
        self.images = []
        self.rect = self.create_rectangle(x,y, x+80,y+80, outline = 'red', width = 1, fill ='blue', alpha = .5)
        self.canvas.tag_bind(self.rect, '<Button-1>', self.onButton)
acw1668
  • 40,144
  • 5
  • 22
  • 34
0

You have to add self. to create_rectangle() and then it will run your function

self.rect = self.create_rectangle(x,y, x+80,y+80, outline='red', width=1, fill='gray', alpha=.5)

Don't add self.canvas. which runs original function in canvas - which doesn't have option alpha

furas
  • 134,197
  • 12
  • 106
  • 148