How can I make Tkinter Label transparent? - python

So hello, here I am once again. This time I am having trouble with Label having white background, even though my picture is transparent. How can I make Label background transparent, because background for the label will have gradients.
Here is simple code(Note that green background is going to have some type of gradient in the future):
from tkinter import *
from PIL import ImageTk, Image
root=Tk()
root.geometry("800x800")
canvas=Canvas(root, width=800,height=800)
canvas.create_rectangle(0,0,800,800, fill="green")
canvas.pack()
ClosePhoto = ImageTk.PhotoImage(Image.open('Close_PreHover.png'))
Close=Label(root,image=ClosePhoto)
Close.place(x=350,y=350)
root.mainloop()
Picture used in code
I know about the canvas.create_image() method, but that won't work for me since some of the commands are binded to the label, where my image is, and few of them change the current image with one slightly different. I have tried the attributes() method with -transparent, but I don't really want a whole in the window.
Any help will be of great value, so thank you in advance!
root.attributes("-transparent",color) method not working

Related

Tkinter Transparent Background macOS

I've been messing around with different code snippets and combinations for a while now, trying to make it so my .gif file would have a transparent background. I've been successful in making the actual background transparent, but I find that my .gif goes transparent with it:
import tkinter as tk
root = tk.Tk()
root.overrideredirect(1)
root.wm_attributes("-topmost", True)
root.wm_attributes("-transparent", True)
root.config(bg='systemTransparent')
root.geometry("+300+300")
root.image = tk.PhotoImage(file="sample.gif")
label = tk.Label(root, image=root.image)
label.config(bg='systemTransparent')
label.pack()
root.mainloop()
I have found that whenever I use both root.config(bg='systemTransparent') and label.config(bg='systemTransparent'), the entire window and gif go transparent, but when I use just one or neither, I am left with a light-gray background.

Is there a way to stop the flashing of buttons with image when clicked in tkinter?

When creating a button with an image on it you can specify its background color to be the same as the root background color with the bg='color' parameter so that if you have an image with transparent background the result seems nice.
However for some reason when you click on the button, there is a white flash that covers the button for as long the click happens.
I have this code:
from tkinter import Tk, Button
from PIL import Image, ImageTk
root = Tk()
root.config(bg='black')
image = Image.open('your_image_file').resize((50, 50))
image = ImageTk.PhotoImage(image)
button = Button(root, width=50, height=50, image=image, bg='black')
button.pack()
root.mainloop()
After importing PIL (if not already) and then filling the appropriate path name to your image the code should run without any problems
Notice that when you click the image a whity flash that covers the button appears?
I want to get rid of that
I've tried passing the parameters highlightcolor highlightbackground and highlightthickness and playing around with them but nothing really changes whether I for example change highlightthickness=0 or highlightbackground='blue'
Is there any way to do that properly?
Thanks in advance!
Oh, I just now realised the activebackground parameter does the job
Everything okay now I can modify the whity flash color :P

Unable to put transparent PNG over a normal image Python Tkinter

So I have 2 images, 1 image is supposed to be the background and the other 1 is just an image of a character.
I am able to put one image over the other but the image has white borders even though it's a PNG file.
This is how it looks like:
This is how I want it to look like:
Here are the two separte images:
https://imgur.com/a/SmE5lgC
Sorry that I didnt post the images directly but I can not since I do no have 10 reputation points.
I've tried converting it to RGBA but same thing happened.
from tkinter import *
from PIL import Image
root = Tk()
root.title("Game")
background = PhotoImage(file="back.png")
backgroundlabel = Label(root, image=background)
backgroundlabel.pack()
character = PhotoImage(file="hero.png")
characterlabel = Label(root, image=character)
characterlabel.place(x=0,y=0)
root.mainloop()
You just need to use the canvas widget in Tkinter. Only the canvas widget supports transparency. This has to do with how Tkinter draws the display. As of right now, your code is just overlaying two images. Tkinter does not know how to compose them with transparency unless you use the canvas widget.
See the following code:
from tkinter import *
from PIL import Image
root = Tk()
root.title("Game")
frame = Frame(root)
frame.pack()
canvas = Canvas(frame, bg="black", width=700, height=400)
canvas.pack()
background = PhotoImage(file="background.png")
canvas.create_image(350,200,image=background)
character = PhotoImage(file="hero.png")
canvas.create_image(30,30,image=character)
root.mainloop()
All I did was download the images you provided. I did not modify the images. So, the bottom line is that you just need to use the canvas widget.
VoilĂ !
Note: The question asked is a duplicate of How do I make Tkinter support PNG transparency?

Tkinter: Display an image on top of other [duplicate]

I am making a chess program and I want to be able to drag the pieces. In order to do this, I put the image of the piece on a Canvas so it can be dragged (I can also use a Label if I want). However, when I drag the piece there is a white square that surrounds the image of the piece.
When I researched the problem, many people gave this solution:
drag_canvas = Canvas(self, height=80, width=80, bg="yellow")
root.wm_attributes("-transparentcolor", "yellow")
This caused the background to be transparent but it was not the chessboard that was visible, it was the program behind the GUI
.
Is there any way I can have the background be transparent and show the chessboard behind rather than the program behind the tkinter window?
Note: I do not mind using any other widget (e.g. a Label) but they must use modules that come default with Python (so no PIL) as this program needs to be used in an environment where I cannot download other modules.
Question: How to make a tkinter canvas background transparent?
The only possible config(... option, to set the background to nothing
c.config(bg='')
results with: _tkinter.TclError: unknown color name ""
To get this result:
you have to hold the chess board and figures within the same .Canvas(....
self.canvas = Canvas(self, width=500, height=200, bd=0, highlightthickness=0)
self.canvas.create_rectangle(245,50,345,150, fill='white')
self.image = tk.PhotoImage(file='chess.png')
self.image_id = self.canvas.create_image(50,50, image=self.image)
self.canvas.move(self.image_id, 245, 100)
Tested with Python: 3.5 - TkVersion: 8.6
A windows only solution is to use the pywin32 module that can be installed with:
pip install pywin32
With pywin32 you can alter the window exstyle and set the canvas to a layered window. A layered window can have a transparent colorkey and is done in the example below:
import tkinter as tk
import win32gui
import win32con
import win32api
root = tk.Tk()
root.configure(bg='yellow')
canvas = tk.Canvas(root,bg='#000000')#full black
hwnd = canvas.winfo_id()
colorkey = win32api.RGB(0,0,0) #full black in COLORREF structure
wnd_exstyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
new_exstyle = wnd_exstyle | win32con.WS_EX_LAYERED
win32gui.SetWindowLong(hwnd,win32con.GWL_EXSTYLE,new_exstyle)
win32gui.SetLayeredWindowAttributes(hwnd,colorkey,255,win32con.LWA_COLORKEY)
canvas.create_rectangle(50,50,100,100,fill='blue')
canvas.pack()
Explaination:
First we need the handle of the window which is called hwnd and we can get it in tkinter by .winfo_id().
Next we get the actual extended window style by GetWindowLong and ask specific for extended style information with win32con.GWL_EXSTYLE.
After that we do a bitwise operation in hexadezimal to alter the style with wnd_exstyle | win32con.WS_EX_LAYERED the result is our new_style.
Now we can set the extended style to the window with SetWindowLong. Finally we have our LayeredWindow which has additional Attributes we can work with. A transparent ColorKey can be set with SetLayeredWindowAttributes while we just use LWA_COLORKEY the alpha parameter has no use to us.
Important note: After defining a transparent colorkey, everything in that canvas with that color will be transparent.

How to add a partially transparent image to Tkinter?

I am making a game in Tkinter and I would really like partially transparent images in the game for obvious reasons...
How would I do that? PNG doesn't allow transparency, and JPEG does allow transparency but isn't considered an 'image file'... What file type would allow that? Can I use bitmap somehow to make a JPEG useable? Thanks!
Some use info:
I started learning Python 3 months ago and am not horribly good, but I do know classes, functions, and how to make stuff in Tkinter. I also obviously know a lot more, but as I said I am not too terribly good at it. Thanks!
Tkinter does recognise .ico files, but I have never used them apart from as an icon at the top left.
I would use a GIF file, and tk.PhotoImage to display it. This does allow for transparent images to be shown:
My Proof:
The image in a tk window, but when I change the background colour to say yellow, the button background is the same as the tk window background, so it is transparent.
EDIT: HERE IS CODE
import tkinter as tk
root = tk.Tk()
image1 = tk.PhotoImage(file = "FILENAME.gif")
label1 = tk.Button(image = image1)
label1.pack()
root.mainloop()
hope that helps you.
from tkinter import *
root = Tk()
startpic = PhotoImage(file = "ddsp.gif")
startbut = PhotoImage(file = "start.gif")
helpbut = PhotoImage(file = "help.gif")
exitbut = PhotoImage(file = "exit.gif")
root.geometry("640x480")
root.title("Deep Death")
root.resizable(0,0)
starter = Label(image = startpic)
starter.pack()
start = Label(image = startbut)
start.place(x=340,y=80)
root.mainloop()
Even though the .gifs' are transparent, they are appearing with a white background.

Categories