Tkinter Transparent Background macOS - python

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.

Related

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

How can I bring an image with a transparent background to the front of my tkinter window?

My image "1_T.png" has a transparent background. I want to bring it to the front of my tkinter window.
I first tried inserting the image in a label. This brought the image to the front of the window but the transparent background was lost because labels can't have transparent backgrounds is my understanding.
I then tried using canvas.create_image(). This helped me preserve the transparent background of my image "1_T.png" but i can't work out how to bring it to the front. I tried canvas.tag_raise(). But that didn't work either.
For example, in the code below, i want to bring the image in front of the label.
Is there a way for me to do that?
import tkinter as tk
HEIGHT = 500
WIDTH = 1000
root = tk.Tk()
canvas = tk.Canvas(root, bg='black', height=HEIGHT, width=WIDTH)
canvas.pack()
mylabel = tk.Label(root, text='testing', bg='#00FFFF')
mylabel.place(relx=0, rely=0, relwidth=0.3, relheight=0.1)
myimage = tk.PhotoImage(file='1_T.png')
myimage2 = canvas.create_image(0, 0, image=myimage)
canvas.tag_raise(myimage2)
root.mainloop()

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?

Widgets disappear when main window moved off screen

I have a tkinter window that I have given a background picture by creating a Label widget with a PhotoImage instance (referencing the image instance through Label attributing).
However when I run the script and move the main window below the start menu (am using Windows 10) or past the sides of the screens for even one moment, all the widgets packed onto the Label (w/ background pic) completely disappear.
They only come back (somewhat) upon hovering over them with the mouse it seems. Also the background picture remains and continues to fill the screen. Could it be that the background picture Label is being "lifted" and makes it seem like the widgets are disappearing? If so, how can I prevent this from happening?
The fix that I have found for now is to not use a Label with a PhotoImage as the parent "frame", but instead use a typical Frame widget with only a background color, but this is not ideal.
import tkinter as tk
root = tk.Tk()
root.geometry('600x350+600+300')
root.resizable(width=False, height=False)
boxBg = '#666'
frameBg = '#fff'
#problem method
backgroundImg = tk.PhotoImage(file='program_media/background.png')
bgFrame = tk.Label(root, image=backgroundImg)
bgFrame.image = backgroundImg
#less than ideal solution so far
#bgFrame = tk.Frame(root, bg='#fff')
bgFrame.pack(expand=1, fill=tk.BOTH)
mainFrame = tk.Frame(bgFrame)
mainFrame.pack(side=tk.TOP)
title = tk.Label(mainFrame, text='Test String')
title.pack(side=tk.TOP)
#widget creation code packed within mainFrame
#...
#... All these widgets (including mainFrame above) are disappearing
#...
#end of widget creation code
root.mainloop()
See what I mean in this screenshot of BEFORE and AFTER moving the main window below the start menu.

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