I am generating PIL Image objects in a loop. I'd like to display these as they are generated with Tkinter.
How would I go about doing this?
You can create a TkInter window containing a Label, and each time you want to change the image, create a new ImageTk.PhotoImage from the old image and set the label's image property to the newly created PhotoImage. Example:
import Tkinter
import Image, ImageTk
root = Tkinter.Tk()
label = Tkinter.Label(root)
label.pack()
def change_image(image):
photoimage = ImageTk.PhotoImage(image)
label.config(image=photoimage)
root.mainloop()
Whenever you need to change the image, call change_image().
Related
instead of using a ico file or blank icon in my tkinter application, i want the tkinter icon to be a 16x16 square with a certain color.
my current code:
import tkinter as tk
from tkinter import *
root = tk.Tk()
icon=PhotoImage(height=16, width=16)
icon.blank()
root.wm_iconphoto('True', icon) #New Tk 8.6 style
root.geometry('707x267')
root.title(" ")
root.mainloop()
how do i change the color of the icon?
You can create your own image with an image processing library, like PIL. First install it:
pip install Pillow
Then create a new image with:
from PIL import Image, ImageTk
img = Image.new(mode='RGB',size=(16,16),color='red')
Now convert the image into tkinter understandable format with:
img_tk = ImageTk.PhotoImage(img)
Next, set the icon to be that new image:
root.wm_iconphoto('True', img_tk)
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.
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()
I am creating an Arduino interface on Sublime using python Tkinter..
I need to show a text over an image. Located in the middle of the screen (512, 200). I don't know how to do it using this library
import Tkinter as tk
from Tkinter import *
root = tk.Tk()
root.geometry("1024x574")
root.title("window")
photo = tk.PhotoImage(file= r"hi.gif")
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=photo, anchor='nw')
text=['my text']
root.mainloop()
Any suggestions?
You need to create a tk label widget and add your text to it. Then you need to use the tk label option compound=.
Taken directly from http://effbot.org/tkinterbook/label.htm:
"compound=
Controls how to combine text and image in the label. By default, if an image or bitmap is given, it is drawn instead of the text. If this option is set to CENTER, the text is drawn on top of the image. If this option is set to one of BOTTOM, LEFT, RIGHT, or TOP, the image is drawn besides the text (use BOTTOM to draw the image under the text, etc.). Default is NONE."
The following is a minimal but working example that accomplishes what you asked for:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
image = Image.open('hi.gif')
tk_image = ImageTk.PhotoImage(image)
label = tk.Label(root, text='Some Plain Text', image=tk_image, compound='center')
label.pack()
root.mainloop()
Im creating a simple chat box in Python and I want to insert an image(emoticons) to a TKinter text widget. I have tried it using this code:
img = Image.open("icon.jpg")
self.bigText.insert(END, img) # bigText is the text widget
Output of the code above is
<PIL.JpegImagePlugin.JpegImageFile instance at 0x01AB5A30>
instead of the image.
I'm not 100% sure on this, but I think you need to use image_create. Something like:
self.bigText.image_create(END, image=img)
should do the trick.
I have made it using:
from Tkinter import *
from PIL import Image, ImageTk
self.myEmoticons.append(self.smiley)
self.bigText.image_create(END,image = self.myEmoticons[self.myEmoticonsCtr])
self.myEmoticonsCtr=self.myEmoticonsCtr + 1