How to create a Label widget with a transparent background using tkinter? - python

I wanted to create an app which has an image as its background. But when I add a Label over the image, the label had a white background.
Is there a way to set the Label widget's background color to 'transparent'?

I'm not aware of a way to make Label backgrounds transparent. One alternative is to use a Canvas widget as the base for the whole thing, then use the create_image method to add the background image, and create_text to make the text labels. It will be a bit more work, but the text should render without a background on top of the image. (Admittedly I have very little experience with the Canvas widget, so I'm speaking more from theory than experience, but it's worth a try.)
If you don't have a good Tkinter reference, I highly recommend this one made by New Mexico Tech. It's available as a downloadable PDF as well.

Related

Opaque text with transparent background in wxpython

In wxpython, you can change the transparency of a frame using 'SetTransparent()' function.
There is a sample app that shows the use of transparent frames here. However, the text also fades as the transparency changes (predictably). Is it possible to make it so the text always remains opaque while the background changes transparency?

python tkinter canvas 'transparent'

I think transparent is the right word I'm looking for. Hopefully my description will explain it properly no matter what.
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)
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.
The obvious thing for Box to be is some kind of widget, like a Frame or Canvas. But widgets don't have transparency.
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.)
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.
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.
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.
Maybe this can give you some ideas to play with:
from tkinter import *
root = Tk()
c = Canvas(root, width=640, height=480, bd=0, highlightthickness=0)
c.create_line(0,240,640,240, fill='blue')
c.pack()
#pil image with transparency
try:
from PIL import Image, ImageTk
except ImportError:
pass
else:
pim = Image.new('RGBA', (5,100), (0,255,0,64))
photo = ImageTk.PhotoImage(pim)
c.create_image(200,200, image=photo, anchor='nw')
#blank standard photoimage with red vertical borders
im = PhotoImage(width=7, height=480)
dat = ('red',)*480
im.put(dat, to=(0,0))
im.put(dat, to=(6,0))
box = c.create_image(0, 0, image=im, anchor='nw')
def on_motion(event):
left,top = c.coords(box)
dx = event.x - (left+7)
c.move(box, dx, 0)
c.bind('<Motion>', on_motion)
root.mainloop()

How do I remove all background color in python tkinter

I think the background color is the issue, however, it might not be. I am trying to make the background of the frame and canvas image so that you can see the text before it is covered by the rounded side.
Here is an image of what I currently have:
http://imgur.com/FIujzHM
There are other spacing issues, but I am focusing on the color of the background needing to be transparent.
Thank you for any help.
You can't...
Configure tkinter/ttk widgets with transparent backgrounds, ttk frame background colors?
After a couple hours of looking stuff up I figured out it is impossible.

Problem with saving image

I have a problem with saving an image. I have following part of code:
self.canvas.postscript(file = filename, colormode = "color")
It works good, but when I set background color in canvas constructor (f.e. bg='red'),
finally image doesn't have this background color. It is still white.
Could anybody help me ?
Sounds like you're using Tkinter: is that right?
I believe the problem is that the bg argument is a general property shared by all widgets. It's really a part of how the widget is drawn on the screen and not a part of the image you're constructing in your canvas. I think the easiest thing for you to do is to draw a red box in your canvas for your background - that will then be included as part of the image saved in your postscript file.

Is it possible to make text translucent in wxPython?

I am adding some wx.StaticText objects on top of my main wx.Frame, which already has a background image applied. However, the StaticText always seems to draw with a solid (opaque) background color, hiding the image. I have tried creating a wx.Color object and changing the alpha value there, but that yields no results. Is there any way I can put text on the frame and have the background shine through? And furthermore, is it possible to make the text itself translucent? Thanks.
You probably need some graphics rendering widget. As far as I know, in wxPython you can use either built-in wxGraphicsContext or pyCairo directly. Cairo is more powerful. However, I don't know the details.
I would try aggdraw into a small canvas.
Any Static Text uses the platform's native label machinery, so you don't get that sort of control over it.

Categories

Resources