Why can't my image load on my tkinter window? - python

I'm trying to add an image to my program but it's not working out, the code is correct as shown below, and the image i'm trying to open is in the same folder as the saved .py file.
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Balance 0-21")
root.configure(width=400, height=200)
root.iconbitmap("C:/Users/user/Desktop/Projects/Balance 0-21/LogoCon.ico")
MasterCard = ImageTk.PhotoImage(Image.open("ten.png"))
MasterCardIMG = Label(image=MasterCard)
MasterCardIMG.grid(row=2, column=3)
root.mainloop()

try adding this after the MasterCardIMG Label creation (also for convention make the first letter lowercase as these aren't classes), I was having the same issue getting my images to show, I believe it has something to do with the label instance creation not being able to assign the image.
MasterCardIMG.image = MasterCard
(recommendation for convention)
masterCard = ImageTk.PhotoImage(Image.open("ten.png"))
masterCardIMG = Label(image=masterCard)
masterCardIMG.image = masterCard
masterCardIMG.grid(row=2, column=3)

Related

The background doesn't round the button tkinter

I am using tkinter to create a GUI. I use PIL to import an image as the background. Here is my code:
root = tk.Tk()
root.title("DFUInfo-v1")
img = ImageTk.PhotoImage(Image.open("background.jpg"))
l=Label(image=img)
l.pack()
root.configure(bg='white')
root.geometry("490x280")
In my app, buttons are rounded. But when I use the image, the background does not match the round buttons, here is the image:
Can somebody help me pls? Thanks
Here is how you can create rounded "buttons" in tkinter (what determines the visible shape is how the image looks):
from tkinter import Tk, Canvas
from PIL import Image, ImageTk
# use your images here
# open your images (my preference is to use `.open` before initialising `Tk`)
img = Image.open('rounded.png')
# resize to match window geometry
bg = Image.open('space.jpg').resize((500, 400))
# the function to call when button clicked
def func(event=None):
print('clicked')
root = Tk()
root.geometry('500x400')
# here are the converted images
photo = ImageTk.PhotoImage(img)
bg = ImageTk.PhotoImage(bg)
# from now on this will be the "root" window
canvas = Canvas(root, highlightthickness=0)
canvas.pack(fill='both', expand=True)
# add background
canvas.create_image(0, 0, image=bg, anchor='nw')
# create button and you have to use coordinates (probably can make
# custom grid system or sth)
btn = canvas.create_image(250, 200, image=photo, anchor='c')
# bind the "button" to clicking with left mouse button, similarly
# as `command` argument to `Button`
canvas.tag_bind(btn, '<Button-1>', func)
root.mainloop()
Most of the explanation is in the code comments, but note that the bound sequence works for the whole image but images are always square so you can click the button being outside of the visible part but only as far as the image goes, it is most certainly possible to create a button that doesn't have such issues but that requires some math
Important (suggestion):
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

Image does not show on button Tkinter

I'm trying to simply add an image to a tkinter button. I tried everything:
import tkinter as tk
root = tk.Tk()
root.geometry('300x300+300+150')
photo = tk.PhotoImage('home.gif')
btn = tk.Button(root, image = photo, width = 100, height = 100)
btn.image = photo # even with this does not work
btn.pack()
root.mainloop()
I also tried with PIL setting the photo variable equal to ImageTk.PhotoImage(Image.open('home.gif')), I tried easly the open function, the absolute path of the photo (and yes, the photo is inside the same directory of my script), but anything works. The window just pop up with a big button, without image inside.
UPDATE:
I tried with other images, and I noticed that some images are shown while others no. This is because the images with transparent background cause a bug or a problem to tkinter... so, I do not know if there's a way to solve this. On google I find out that some people use canvas but I actually need the image to be inside the button so I do not know how to do.
Please change your code as below
photo = tk.PhotoImage(file='home.gif')
because i changed the above code and it worked....

How to display an image in tkinter using grid

I am trying to display an image to my GUI, through PhotoImage, however it is telling me that PhotoImage has no "grid" member.
I am using .grid() for all of my other widgets, like labels and buttons, so I can't use .pack(), if that would make a difference anyway. So, how could I display an image to my GUI with grids?
I don't know if code is necessary for this question, but
# it works by getting a random image from a list of files
Label(root, text=f"Enter a number between one and {len(files)}")
self.entry = Entry(root)
self.entry.grid()
Button(root, text="Submit", command=self.getEntry).grid()
Then, getEntry() is:
def getEntry(self):
PhotoImage(name="",
file=f{self.path}\\{self.showImage(self.entry.get())}).grid(row=1)
"""
showImage() just returns the correct file
(e.g: files[index] where index is the input)
"""
NOTE:
grid() is returning NoneValue, which means that your variable 'file' will be NoneValue.
To display an image in tkinter you'll need to do something like:
from PIL import Image, ImageTk
image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)
label = Label(root, image = photo)
label.image = photo
label.grid(row=1)
# label.pack()
NOTE:
The PhotoImage class can read GIF and PGM/PPM images from files:
photo = PhotoImage(file="image.gif")
photo = PhotoImage(file="lenna.pgm")
If you need to work with other file formats, the Python Imaging
Library (PIL) contains classes that lets you load images in over 30
formats, and convert them to Tkinter-compatible image objects:
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
You can use a PhotoImage instance everywhere Tkinter accepts an image
object.
An example:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
You must keep a reference to the image object in your Python program, either by storing it in a global variable, or by attaching it to another object.
You have to use a widget that can support images. Typically a label, but button can also configure a button to show an image, as well as add images to text and canvas widgets.

How to hide / show a GIF image in Tkinter

In using Python's Tkinter application, I have come across a little problem. How do I hide and show GIF images that I have put into the window using PhotoImage?
I can make the images appear in the first place, but am unable to do anything with them. I have tried using canvas.itemconfig and canvas.update, but to no avail. Is anyone able to solve this problem?
picture = PhotoImage(file='C:\\Users\\ZecFamily5\\Downloads\\Island.gif')
c.create_image(250, 250, image=picture)
The above code cannot be hidden or shown using c.itemconfig(image, state=HIDDEN).
You should do .itemconfig(Your Item Name, state=HIDDEN) and to show, change HIDDEN to NORMAL to show.
I would recommend using the ImageTK module:
img = ImageTk.PhotoImage(Image.open(path))
As it should allow you to make a TKinter compatible photo image.
For this to work though, do keep in mind that this will need to be at the beginning of your file:
import Tkinter as tk
from PIL import ImageTk, Image
Here is an example piece of code to show you what I mean:
import Tkinter as tk
from PIL import ImageTk, Image
path = 'C:/xxxx/xxxx.jpg'
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

3.4 Python changing image position tkinter

I am making a small pop up to a text game I am making. I just want a small frame to pop up with some text and a picture to the far left but in a custom size and position. How would I do this? How do I insert text and change the image position and size, this is my current code, it works as in there are no errors and the image shows.
from tkinter import *
import time, sys
root = Tk()
root.configure(background='#16e116')
root.title('Pop Up')
root.geometry('300x200')
photo = PhotoImage(file='file.gif')
w = Label(root, image=photo)
w.pack()
root.mainloop()
If you want your image to be a different size (without it being deformed by resizing the widget) you have to make it the desired size in your image editor.
If you want to change the image position then use grid() where pack() has very limited placement options.
If you want text on a label, then use: text = Label(root, text="Hello world!"). This also comes with various options and styles such as font and it's size.
If you want the text over the image then use grid() and place it in the middle of the image by using rows and columns.
import tkinter
from tkinter import *
root = tkinter.Tk()
root.configure(background='#16e116')
root.title('Pop Up')
root.geometry('300x200')
photo = PhotoImage(file='file.gif')
w = Label(root, image=photo)
text = Label(root, text="Hello world!")
w.grid(row=3, column=3)
text.grid(row=3, column=3)
root.mainloop()
Although, the label will still have the background around it but you change the background colour using widgetname.config(bg="Colour")to make it suit your standards.
Now of course, I can't be sure the above will work because I don't have file.gif but you should also read tutorials before jumping straight into code to avoid confusion.

Categories

Resources