如何使用tkinter在透明窗口的不同位置创建3个(红色)不透明矩形轮廓? [英] How to create a 3 (red) opaque rectangular outlines at different location over a transparent window using tkinter?

查看:53
本文介绍了如何使用tkinter在透明窗口的不同位置创建3个(红色)不透明矩形轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在透明窗口上放置三个单独的红色框.

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

我使用的第一种方法是依靠三个透明背景的矩形框的图片(因为我是tkinter的新手),然后将其放在透明窗口上.

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.

推荐答案

这是一个绝妙的问题....

This was a brilliant question....

我想出了解决这个问题的方法.-alpha属性用于调整整个窗口的透明度,并且仅调整窗口的某些部分(小部件)的透明度,我们使用-transparentcolor属性.我们要做的是将一种颜色指定为透明颜色,每当我们使用与任何小部件的bg颜色相同的颜色时,它将自动使该特定小部件的颜色透明.

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.

这是我的代码.

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()

输出-

希望这对您有帮助...

I hope this helped you...

这篇关于如何使用tkinter在透明窗口的不同位置创建3个(红色)不透明矩形轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆