HKVariant
HKVariant

Reputation: 867

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!

Upvotes: 2

Views: 4969

Answers (2)

HKVariant
HKVariant

Reputation: 867

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.

Upvotes: 1

W1ll1amvl
W1ll1amvl

Reputation: 1269

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:

enter image description here

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.

enter image description here

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.

Upvotes: 5

Related Questions