0

I have the following GUI as shown in the Figure below. Instead of the black box, I wanted to draw a transparent rectangle on top of all the scales covering values 1, 0, and -1 of the scale.

Is there a way to make the Tkinter canvas transparent? If it is not the correct way to do it, what are the alternatives that I could try? I shared the sample code I use. That can be used to reproduce this GUI.

enter image description here

from tkinter import *
import itertools

root = Tk()
root.geometry('840x420')
root.title('Test Window')

variables = {"var1", "var2", "var3", "var4"}
pair_list = list(itertools.combinations(list(variables), 2))
pair_result_dictionary = dict.fromkeys(pair_list)

my_canvas = Canvas()
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)

my_scrollbar = Scrollbar(root, orient=tk.VERTICAL, command=my_canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)

my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))

second_frame = Frame(my_canvas)

my_canvas.create_window((0, 0), window=second_frame, anchor="nw")

i = 0

heading_label = Label(second_frame, text="Test Window", font=('Arial',16))
heading_label.grid(column=0, row=0, sticky=tk.NW, columnspan=2, padx=(52, 0), pady=(20, 10))

for pair in pair_list:
    sample_scale = tk.Scale(second_frame, from_=9, to=-9, length=600, orient=tk.HORIZONTAL, font=('Arial', 15),
                            tickinterval=1,resolution=1)

    label_left = tk.Label(second_frame, text=pair[0], font=('Arial', 15))
    label_left.grid(column=0, row=2 + i, sticky=tk.W, padx=(52, 0), pady=5)

    sample_scale.set(((sample_scale['from'] - sample_scale['to']) / 2) + sample_scale['to'])
    sample_scale.grid(column=1, row=2 + i, sticky=tk.W, padx=(5, 0), pady=5)

    label_right = tk.Label(second_frame, text=pair[1], font=('Arial', 15))
    label_right.grid(column=2, row=2 + i, sticky=tk.W, padx=(5, 5), pady=5)

    i = i + 100

rectangle_holder_canvas = tk.Canvas(second_frame, width=100, height=70, bd=0, background='#000000')
rectangle_holder_canvas.grid(column=1, row=2, sticky=tk.S, padx=(0, 0), pady=0)
rec = rectangle_holder_canvas.create_rectangle(3, 3, 100, 70, outline='blue', fill='')
rectangle_holder_canvas.tag_raise(rec, 'all')

root.mainloop()

If I use the following code lines, it makes the entire square transparent and will see what's underneath the main window which is not what I want sadly.

root.wm_attributes("-transparentcolor", 'grey')

rectangle_holder_canvas = tk.Canvas(second_frame, width=100, height=70, bd=0, bg="grey")

Appreciate your thoughts and time on how to achieve this.

I read that, "tag_raise" method does not affect canvas window items. To change a window item's stacking order, use a lower or lift method on the window.. So I tried to draw a rectangle on my_canvas by setting second_frame.lower() as shown below code. Then I only see the rectangle but not the scales or labels on the second frame.

my_canvas.create_window((0, 0), window=second_frame.lower(), anchor=CENTER)
rec = my_canvas.create_rectangle(50, 50, 200, 200, outline='blue', fill='blue')
Mad
  • 337
  • 1
  • 15
  • Are you looking for one of these [answers](https://stackoverflow.com/q/53021603/13629335) – Thingamabobs Jun 04 '22 at 07:53
  • Thanks, I saw this question and the accepted answer says to use the same canvas. But it did not work in my case. I explained this in the last bit of my question. – Mad Jun 04 '22 at 09:14
  • May I ask you what the purpose of this is ? Maybe there is a better way. – Thingamabobs Jun 04 '22 at 10:07

0 Answers0