How to create a 3 (red) opaque rectangular outlines at different location over a transparent window using tkinter?

。_饼干妹妹 提交于 2020-07-09 14:09:26

问题


I wanted to place three separate red boxes over a transparent window.

This first method I used relied on a picture of three rectangular boxes with a transparent background ( because I'm new to tkinter) and then place it over the transparent window.

from PIL import ImageTk, PngImagePlugin
from tkinter import *

import tkinter as tkr

app = tkr.Tk()
app.title("AI Cashier")
app.geometry("1366x768")
app.wm_attributes("-alpha", 0.1)


my_img = ImageTk.PhotoImage(PngImagePlugin.Image.open("Capture1.png"))
my_label = Label(image= my_img)
my_label.pack()

app.mainloop()

However, the end result gives me transparent red rectangles as well..... So could you help me with this one?

I wanted to try the second method where you use the code to draw the red rectangles instead of using pictures, but I don't really know how to...

I bet this is a dumb question, but thanks to anyone who wants to answer this.


回答1:


This was a brilliant question....

I have come up with a solution to this problem. The -alpha attribute is used to adjust the transparency of the entire window, and to adjust the transparency of only certain parts(widgets) of the window we use the -transparentcolor attribute. What we do is we assign a color as our transparent color and whenever we'll use this same color as the bg color of any widget, it will automatically make that particular widget transparent in color.

Here's my code..

import tkinter as tkr
app = tkr.Tk()
app.title("AI Cashier")
app.geometry("1366x768")
app.wm_attributes("-transparentcolor", "white")  #the color "White" will now be used to represent a transparent background
app.config(bg = "White")
can = tkr.Canvas(app,bg = "White",highlightthickness = 0)
can.create_rectangle(100,50,160,100,outline = "Black",fill="red",width = 2)
can.create_rectangle(180,50,240,100,outline = "Black",fill="red",width = 2)
can.create_rectangle(260,50,320,100,outline = "Black",fill="red",width = 2)
can.pack()
app.mainloop()

Output -

I hope this helped you...



来源:https://stackoverflow.com/questions/62491578/how-to-create-a-3-red-opaque-rectangular-outlines-at-different-location-over-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!