python tkinter画布“透明" [英] python tkinter canvas 'transparent'

查看:90
本文介绍了python tkinter画布“透明"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为透明是我正在寻找的正确词.希望我的描述无论如何都能正确解释.

I think transparent is the right word I'm looking for. Hopefully my description will explain it properly no matter what.

我有一张可追溯到几十年前的基于时间的数据图表.几乎简单地设置为每 x 像素显示一天,除非放大.我想要一个盒子,也许沿着 5x100 的线,出现在图形的顶部,所以当我将鼠标移到图形上时,盒子会移动并与鼠标保持同步.另一个词显示了最远的 5 天x 天数之前"发生的事情.换句话说,在计算向前的平均值时,随着新数据的到来,下一个值是什么.自然地,我希望底层的图形数据与上面的透明框一起显示,概述了有问题的日子.这可能会变得足够疯狂,成为一个更宽的框,其中有两个浅灰色或类似颜色的区域以显示有问题的区域,但彩色区域相隔数天(也可能是多个透明窗口,它们也被跟踪在一起). 这对 tkinter 可行吗?从我一直在做的研究来看,如果使用

I have a graph of time based data going back decades. Pretty much simply setup to show one day per x-pixel, unless zoomed in closer. I would like to have a box, maybe along the lines of 5x100, appear on top of the graph so when I move the mouse over the graph the box will move and keep pace with the mouse. Anotherwords showing what was happening in the 5 furthest days 'x number of days prior'. Anotherwords when computing an average going forward what are the next values to be falling off as new data arrives. Naturally I want the underlying graphed data to be displayed with the transparent box on top of it outlining the days in question. This may get crazy enough to be a much wider box with two areas that are colored light grey or something like that to show the areas in question but the colored areas are separated by numerous days(could be multiple transparent windows that are tracked together as well. Is this feasible with tkinter? From the research I've been doing it's questionable if using

root.attributes('alpha', .30)

行不行.听起来我不能做类似的事情,因为它最终会使图表对其下方的任何内容透明.

would work or not. It doesn't sound like I could do something like as it would end up making the graph transparent to whatever is underneath it.

self.Graph.create_line()
self.Box.attributes('alpha', .30)

如果我理解正确,我必须使用

If I understand correctly I have to use

attributes

就在根级别而不是单个窗口"级别,因此上述(严重削减)代码将不起作用......或者它会.我还没有机会尝试任何东西,看看会发生什么……那将在今晚晚些时候.有点希望通过现在询问来节省自己的时间,而您永远不知道有时还有谁可能需要帮助.

right at the root level versus the individual 'window' level so the above (severely chopped down) code wouldn't work...or would it. I haven't had a chance to try anything out yet to see what happens...that will be later on this evening. Kinda hoping to save myself a little time by asking now and you never know who else may need the help sometime.

推荐答案

如果我理解你想要做什么,那将会非常困难.

If I understand what you're trying to do, it's going to be pretty hard.

设置窗口属性可以很好地使窗口透明.您在代码中遇到了一个小问题——属性以连字符开头——但除此之外,您做对了:

Setting window attributes works just fine to make a window transparent. You've got a minor problem in the code—attributes start with a hyphen—but otherwise you've got it right:

self.Box.attributes("-alpha", .30)

但是,听起来您希望 Box 成为图形窗口的嵌入部分,而不是它自己的可以由用户拖动的顶级窗口等.不幸的是,Tkinter没有任何关于子窗口的概念,并且它没有暴露足够多的本地内容,您需要通过创建一个固定窗口并手动移动它来跟踪另一个窗口的移动来伪造它们.因此,您没有窗口,这意味着您没有窗口透明度.

However, it sounds like you want that Box to be an embedded part of the graph window, not its own top-level window that can be dragged around by the user, etc. Unfortunately, Tkinter does not have any notion of child windows, and it doesn't expose nearly enough of the native stuff you'd need to fake them by creating an immobile window and manually moving it to track the movements of another window. So, you don't have a window, which means you don't have window transparency.

Box 显然是某种小部件,例如 FrameCanvas.但是小部件没有透明度.

The obvious thing for Box to be is some kind of widget, like a Frame or Canvas. But widgets don't have transparency.

Box 可以只是在与 Graph 相同的 Canvas 上绘制的元素集合.这看起来很有希望……但是 Canvas 方法都没有处理 alpha 透明度.(其中一些确实处理全有或全无的透明度,但这无济于事.)

Box could instead be just a collection of elements drawn onto the same Canvas as the Graph. That seems promising… but none of the Canvas methods handle alpha transparency. (Some of them do handle all-or-nothing transparency, but that doesn't help.)

处理透明度的一件事是PhotoImage.因此,如果您在屏幕外绘制 Box,将结果内容作为 PhotoImage,添加 alpha(例如,通过 PIL),然后添加 create_image 结果……接近,但没有雪茄.根据底层 Tk 库的设置,Tkinter 可能只绘制具有 1 位透明度的像素图或完全忽略透明度.(在 PIL 中加载 alpha 透明的 PNG 文件并在 Canvas 上绘制它们的实验.)因此,除非您想要一个在某些系统上看起来正确的应用程序,否则根本不会在其他系统上绘制 Box,而在其他系统上绘制它是不透明的,这是一个死胡同.

One thing that does handle transparency is PhotoImage. So, if you draw Box off-screen, get the resulting contents as a PhotoImage, add the alpha (e.g., via PIL), then create_image the result… close, but no cigar. Depending on the settings of the underlying Tk library, Tkinter may just draw the pixmap with 1-bit transparency or ignore transparency completely. (Experiment with loading alpha-transparent PNG files in PIL and drawing them on a Canvas.) So, unless you want an app that looks right on some systems, doesn't draw the Box at all on others, and draws it opaque on others, this is a dead end.

那么,还剩下什么?仅手动合成:在单独的屏幕外窗口上绘制 GraphBox,获取像素图,使用 PIL 合成它们,然后 create_image结果.

So, what's left? Only manual compositing: Draw the Graph and the Box on separate off-screen windows, get the pixmaps, use PIL to compose them, and create_image the result.

在这一点上,您最好首先使用 PIL 的 ImageDraw 之类的东西或更强大的库来构建像素图.或者,当然,使用比 Tk 更强大的 GUI 库,例如 Qt 或 wx.

At which point you're probably better off just using something like PIL's ImageDraw or a more powerful library to construct the pixmap in the first place. Or, of course, using a more powerful GUI library than Tk, like Qt or wx.

这篇关于python tkinter画布“透明"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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