Python Tkinter - Menu icon doesn't show - python

I'm building a menu with tkinter but the icons don't show.
Can you help me?
mb=Menu(w)
w.config(menu=mb)
fm=Menu(mb,tearoff=0)
om=Menu(mb,tearoff=0)
hm=Menu(mb,tearoff=0)
mb.add_cascade(label=_("File"),menu=fm)
fm.add_command(label=_("Esci"), image=PhotoImage(r"icons\exit.png"),
compound="left",command=w.destroy)
fm.iconPhotoImage = PhotoImage(r"icons\exit.png")
mb.add_cascade(label=_("Opzioni"),menu=om)
om.add_command(label=_("Impostazioni"), image=PhotoImage(r"icons\settings.png"),
compound="left", command=settings.creaFinestra)
om.add_command(label=_("Cambia lingua"), image=PhotoImage(r"icons\language.png"),
compound="left", command=settings.cambiaLingua)
mb.add_cascade(label=_("Aiuto"), menu=hm)
hm.add_command(label=_("Guida"), image=PhotoImage(r"icons\help1.png"),
compound="left",
command= lambda: webbrowser.open("https://github.com/maicol07/school_life_diary_pc/wiki"))
hm.add_command(label=_("Informazioni"), image=PhotoImage(r"icons\help.png"),
compound="left",command=info)

As explained here, for such images formats, you need to use the PIL library which converts them to Tkinter-compatible image objects:
from PIL import Image, ImageTk
image = Image.open("icons\exit.png")
photo = ImageTk.PhotoImage(image)
Then attach it to your widget:
fm.add_command(label=_("Esci"), image=photo, ...)
You need to repeat this process for each .png image you used.

Related

How do I use an image for a project using PIL in Python code?

I am trying to make a blackjack game in Python using Tkinter and PIL packages. I saved all the necessary cards in a folder on my desktop but cannot seem to get the card images to appear.
Below is the code I have tried :
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Blackjack")
canvas = Canvas(root, bg="green", height=1200, width=800)
canvas.pack(fill=BOTH, expand=1)
img = ImageTk.PhotoImage(Image.open(
"C:\Desktop\GUI\Images\Cards\2_of_clubs.png"))
root.mainloop()
How to make images appear when using tkinter ?

How to display an image (on a canvas) selected via filedialog?

I started a project a few days ago but unfortunately I'm stuck. I would like to make an image editor (a very simple one ;D) where a choose an image by using the filedialog then I would have the possibilty to make few modifications like rotations. My problem is that I can choose the image but once I did, I can't show the image on the canvas.
It says : "name 'image' is not define"
I think my problem is that the program want to show the image on the canvas but I haven't selected it yet.
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
root = Tk()
#function to select my image by using the filedialog
def select_image():
file_path = filedialog.askopenfilename()
image = Image.open(file_path)
#button to press to open filedialog
select = Button(root, text="select an image", command=select_image)
select.pack()
#the canvas where the image will be display
canvas = Canvas(root, width= 100, height=100, bg="grey")
canvas.pack()
image_tk = ImageTk.PhotoImage(image)
canvas.create_image(0,0, image= image_tk)
root.mainloop()
It is possible to create an image object before you have an image file to display, but not in the way you're doing it. You simply need to create an empty image object and keep track of the image object id, and then reconfigure that object inside of select_image.
For example, don't define image_tk in the main program. Change the line that creates the image item on the canvas to this:
image_id = canvas.create_image(0,0, anchor="nw")
(note: without the anchor option, the center of the image will be at 0,0. I'm guessing you want the upper-left corner of the image to be at 0,0).
Next, in select_image is where you do all of the work of getting the image, saving a reference to it (to avoid it being deleted when the function returns), and showing it in the canvas. It would look something like this:
def select_image():
# ask the user for the filename
file_path = filedialog.askopenfilename()
# only show the image if they chose something
if file_path:
# open the file
image = Image.open(file_path)
# create the image object, and save it so that it
# won't get deleted by the garbage collector
canvas.image_tk = ImageTk.PhotoImage(image)
# configure the canvas item to use this image
canvas.itemconfigure(image_id, image=canvas.image_tk)
You’ve got a couple of problems here:
You are never calling your function! Your code ignores
select_image() after it is defined
The variable image is only defined inside your (uncalled) function, so when you try to use it via ImageTk.PhotoImage(), it is
undefined.
Try returning the image object this way:
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
root = Tk()
#function to select my image by using the filedialog
def select_image():
file_path = filedialog.askopenfilename()
return Image.open(file_path)
#button to press to open filedialog
select = Button(root, text="select an image", command=select_image)
select.pack()
#the canvas where the image will be display
canvas = Canvas(root, width= 100, height=100, bg="grey")
canvas.pack()
image_tk = ImageTk.PhotoImage(select_image())
canvas.create_image(0,0, image= image_tk)
root.mainloop()
Note that you are not checking for errors or cancellation in your select_image() function. You’d be better off handling cancel or error inside the function too

Python import image gif

Good morning, everyone,
I tried to import image logo.gif but got an error message saying the image does not exit.
Appreciate your help.
Dennis
from PIL import Image, ImageChops
from PIL.GifImagePlugin import getheader, getdata
img = Image.open("logo.gif")
img.show()
from tkinter import *
window = Tk()
imgLbl = Label( window, image = img )
TclError: image "<PIL.GifImagePlugin.GifImageFile image mode=P size=92x95 at 0x8C92B38>" doesn't exist
Please refer to this post Play Animations in GIF with Tkinter
This question seems to be a duplicate.
You'll have to first convert the image to tK compatible image by the function PhotoImage present in ImageTk class.

How can I open an image in Python?

I have looked at a lot of tutorials tried them all, but nothing seemed to work through Pygame, PIL, Tkinter. it could be because of me of course, cause Im a greenie...
from Tkinter import *
root = Tk()
photo = PhotoImage(file="too.jpg")
label = Label(root, image=photo)
label.pack()
root.mainloop()
Your code is correct but it won't work because of the jpgfile.
If you want to use the PhotoImage class you can only read read GIF and PGM/PPM images from files (see docs).
For other file formats you can use the Python Imaging Library (PIL).
Here's your example using PIL:
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
image = Image.open("too.jpg")
photo = ImageTk.PhotoImage(image)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
root.mainloop()
The line label.image = photo is necessary if you want to avoid your image getting garbage-collected.

Adding image to Tkinter

I have added a image file to my code in tkinter but it basically fills my the whole frame so if its possible can you recommend a tutorial that shows or explains how to do this.... unless you can show me on here.
I havent added my full code but the code below should display a test image once you have it saved in the python directory.
I would like to create 'next' button which would open up a new frame with another image on it.
from Tkinter import *
root = Tk()
ButtonImage = PhotoImage(file='test.gif')
testButton = Button(root, image=ButtonImage)
testButton.pack()
root.mainloop()
You could try something like this:
from Tkinter import *
from glob import glob
class ImageFrame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.images = glob("*.gif")
self.cur = 0
# label showing the image
self.image = PhotoImage()
imagelabel = Label(self, image=self.image)
imagelabel.grid(row=1, column=1)
# button cycling through the images
button = Button(self, text="NEXT", command=self.show_next)
button.grid(row=2, column=1)
# layout and show first image
self.grid()
self.show_next()
def show_next(self):
self.cur = (self.cur + 1) % len(self.images)
self.image.configure(file=self.images[self.cur])
ImageFrame().mainloop()
Some explanations:
glob is used to get a list of all files matching some pattern in the current directory
grid is a simple but quite flexible layout manager for Tkinter (see Tkinter reference)
the show_next method, which is bound to the button, cycles through the images and binds a new image to the PhotoImage using configure
The result is a simple frame showing a large image and a button, cycling though the gif images in the current directory.
There are numerous modules that would help you out with this. You can use the PIL module. Typically what I would do in a situation like yours is use the PIL module to load and paste the image onto the frame. This is how you do this.
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
Image = Image.open(path).resize((300, 300)), Image.ANTIALIAS
ButtonImage = ImageTk.PhotoImage(Image)
# If you are using image by itself. Without it being a button.
#Image_Label = Label(image = self.HomeImage, borderwidth=0, highlightthickness=0)
# Otherwise
testButton = Button(root, image=ButtonImage)
testButton.pack()
root.mainloop()
I believe that this would definitely help you with resizing the image and loading an image to the screen as a button. We used PIL to load the image onto the frame and resized the image. This is What you were also asking for earlier. I used the resize method on the Image.open() Function. This resizes the image to what you want. The standards are the actual sizes of that image.

Categories

Resources