Image in TreeView not showing Tkinter - python

I am making a TreeView in Tkinter Python 3.4 I have added a chrome logo but the image never appears.
treeview=ttk.Treeview(frame3)
chromelogo=PhotoImage(file="./Images/minor-logo.png")
chromelogo=chromelogo.subsample(10,10)
treeview.pack(fill=BOTH,expand=True)
treeview.insert('','0','Chrome',text='Chrome', image=chromelogo)
treeview.insert('Chrome','0',"shit",text="shit",image=chromelogo)
treeview.insert('','1','Chrome3',text='Chrome3')
The link for chrome logo: http://logos-download.com/wp-content/uploads/2016/05/Chrome_icon_bright.png

Photoimage doesn't let you open images rather that a gif type if you want to open an image rather than gif in tkinter try the PIL module. you could use any method to install the PIL module, i like the command line
python -m pip install pillow
since you want to your image to feet in the tree view use this to resize and insert it
from tkinter import *
from tkinter import ttk
from PIL import ImageTk,Image
win=Tk()
chromelogo=Image.open("./Images/minor-logo.png")#open the image using PIL
imwidth=10#the new width you want
#the next three lines of codes are used to keep the aspect ration of the image
wpersent=(imwidth/float(chromelogo.size[0]))
hsize=int(float(chromelogo.size[1])*float(wpersent))#size[1] means the height and the size[0] means the width you can read more about this in th PIL documentation
chromelogo=ImageTk.PhotoImage(chromelogo.resize((imwidth,hsize),Image.ANTIALIAS))# set the width and put it back in the chromelogo variable
treeview=ttk.Treeview(win)
treeview.pack(fill=BOTH,expand=False)
treeview.insert('','0','Chrome',text='Chrome', image=chromelogo)
treeview.image = chromelogo#this one is for telling tkinter not to count the image as garbage
treeview.grid(row=0,rowspan=2,columnspan=2,padx=220,sticky=N+W,pady=20)
chromelogo2=ImageTk.PhotoImage(Image.open("./Images/minor-logo.png"))
treeview.insert('Chrome','0',"shit",text="shit",image=chromelogo2)#here you can also insert the unresized logo so you could see it as big as it is
treeview.insert('','1','Chrome3',text='Chrome3')
win.mainloop()

Simply converting "chromelogo" into a class variable by using the self keyword should fix the problem:
treeview=ttk.Treeview(frame3)
self.chromelogo=PhotoImage(file="./Images/minor-logo.png")
self.chromelogo=self.chromelogo.subsample(10,10)
treeview.pack(fill=BOTH,expand=True)
treeview.insert('','0','Chrome',text='Chrome', image=self.chromelogo)
treeview.insert('Chrome','0',"shit",text="shit",image=self.chromelogo)
treeview.insert('','1','Chrome3',text='Chrome3')

Related

How to display an image in a specific tkinter window using PhotoImage?

I have two running tkinter windows but I want only one specific window to display the image but I am not able to achieve this. I tried to specify the master in the Label statement but python shows an error which says "image pyimage1 doesn't exist"
Please help
import tkinter as tk
from PIL import Image, ImageTk
a=tk.Tk()
a.geometry('800x500+275+100')
a.title('HOME PAGE')
c=tk.Tk()
c.geometry('800x500+275+100')
c.title('PROFILE')
load=Image.open('untitled.png')
render=ImageTk.PhotoImage(load)
img=tk.Label(c,image=render)
img.pack()
a.mainloop()
c.mainloop()
If you want a second screen use tk.Toplevel and remove c.mainloop
a=tk.Tk()
a.geometry('800x500+275+100')
a.title('HOME PAGE')
c=tk.Toplevel()
c.geometry('800x500+275+100')
c.title('PROFILE')
load=Image.open('untitled.png')
render=ImageTk.PhotoImage(load)
img=tk.Label(c,image=render)
img.pack()
a.mainloop()

How do I open .gif file using tkinter without getting error "Too early to create image"?

I cannot start my Python program. I've a problem that I cannot open a .gif file, and I cannot figure out how!
I keep getting a long error message:
"RuntimeError: Too early to create image"
I have moved the gif files into the same project file as the code, and I tried looking online, but everyone uses different packages, and I just cannot find a way around it. I also have the gifs open on pycharm.
Here is my code:
import random
from tkinter import *
sign = random.randint(0, 1)
if (sign == 1):
photo = PhotoImage(file="X.gif")
else:
photo = PhotoImage(file="O.gif")
My overall goal is to show an image like a finished tic tac toe game, with randomly placed X's and O's, and there does not have to be any specific order like 3 in a row. Here is the homework problem:
Display a frame that contains nine labels. A label may display an image icon for X or an image icon for O, as shown in Figure 12.27c. What to display is randomly decided.
Use the Math.random() method to generate an integer 0 or 1, which corresponds to displaying an X or O image icon. These images are in the files x.gif and o.gif.
I can see from the code that you're using PhotoImage before creating a main window gives you an Runtime error and it is clearly said in the error that "Too early to create image" means the image cannot be create if there is no active Tk window.
The reason why some people prefer the use other module because it give you more flexibility to resize, reshape, invert and more. ( By the way it could Pillow module from PIL import Image, ImageTk How to use PIL in Tkinter ).
Now back to your code.
You can randomise "O" and "X" images without even use of if-else.
I created main window before creating the Image.
Make sure the images you using are in the same directory.
import random
from tkinter import *
sign = random.choice( ["X.gif", "O.gif"] )
print(sign,"photo has been selected")
root = Tk()
Photo = PhotoImage(file=sign)
display_photo = Label(root, image=Photo)
display_photo.pack()
mainloop()

pypdfocr issue: 'pdfimages' is not recognized as an internal or external command

I have been trying to convert a pdf to an image and display on a tkinter canvas. I have it working but every time i convert a pdf i get this error(I borrowed this code from here (PDF Viewer for Python Tkinter). Is there anyway to just set it to 300DPI? or better yet why is this happening and what can i do about it? I have searched for hours trying to find other convert pdf to image solutions and came up with nothing I could figure out easily(I am quite new to python).
pypdfocr issue: 'pdfimages' is not recognized as an internal or external command, operable program or batch file. WARNING: Could not execute pdfimages to calculate DPI (try installing xpdf or poppler?), so defaulting to 300dpi
import pypdfocr.pypdfocr_gs as pdfImg
from PIL import Image, ImageTk
import Tkinter as tk
import ttk
import glob, os
root=tk.Tk()
__f_tmp = glob.glob(pdfImg.PyGs({}).make_img_from_pdf("1.pdf")[1])[0]
# ^ this is needed for an "default"-Config
__img=Image.open(__f_tmp)
__tk_img = ImageTk.PhotoImage(__img.resize((800,1000), Image.ANTIALIAS))
ttk.Label(root, image=__tk_img).grid()
__img.close()
os.remove(__f_tmp)
root.mainloop()

Python 2.7 How to add images on the canvas with Tkinter

I need to add an image to the canvas. I have tried countless amounts of things, and finally decided to make a question on here.
This is what I have imported
from Tkinter import *
import tkFont
from PIL import ImageTk, Image
And this is the line of code I'm trying to add to import an image from the same folder the main file is in.
c.create_image(100,100, anchor=N, image = ghost.jpg)
I've also tried putting ""s around 'ghost.jpg' and it says the Image does not exist then. Without the quotes it says "global name 'ghost' does not exist."
Can anyone help?
Canvas.create_image's image argument
should be a PhotoImage or BitmapImage, or a
compatible object (such as the PIL's PhotoImage). The application must
keep a reference to the image object.
from Tkinter import *
"""python 2.7 =Tkinter"""
from PIL import Image, ImageTk
app = Tk()
temp=Image.open("photo.jpg")
temp = temp.save("photo.ppm","ppm")
photo = PhotoImage(file = "photo.ppm")
imagepanel=Label(app,image = photo)
imagepanel.grid()
app.mainloop()
This is a bit of code I wrote in python 2.7 with Tkinter and PIL to import a jpg file from a given directory, this doesn't pop up off the screen.
You should replace photo with the file name (and directory) and app with the relevant variable you set.

Setting Application icon in my python Tk base application (On Ubuntu)

I want to set an image in my GUI application built on Python Tk package.
I tried this code:
root.iconbitmap('window.xbm')
but it gives me this:
root.iconbitmap('window.xbm')
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1567, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "window.xbm" not defined
Can anyone help?
You want to use wm iconphoto. Being more used to Tcl/Tk than Python Tkinter I don't know how that is exposed to you (maybe root.iconphoto) but it takes a tkimage. In Tcl/Tk:
image create photo applicationIcon -file application_icon.png
wm iconphoto . -default applicationIcon
In Tk 8.6 you can provide PNG files. Before that you have to use the TkImg extension for PNG support or use a GIF. The Python PIL package can convert images into TkImage objects for you though so that should help.
EDIT
I tried this out in Python as well and the following worked for me:
import Tkinter
from Tkinter import Tk
root = Tk()
img = Tkinter.Image("photo", file="appicon.gif")
root.tk.call('wm','iconphoto',root._w,img)
Doing this interactively on Ubuntu resulted in the application icon (the image at the top left of the frame and shown in the taskbar) being changed to use my provided gif image.
This worked for me
from tkinter import *
raiz=Tk()
raiz.title("Estes es el titulo")
img = Image("photo", file="pycharm.png")
raiz.tk.call('wm','iconphoto',raiz._w, img)
raiz.mainloop()
Try this:
root.iconbitmap('#window.xbm')
And quote:
Set (get) the icon bitmap to use when this window is iconified. This method are ignored by some window managers (including Windows).
Note that this method can only be used to display monochrome icons. To display a color icon, put it in a Label widget and display it using the iconwindow method instead.

Categories