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()
Related
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')
I know there are lot of similar questions, but there aren't any simple enough that I am able to understand. I have the following code:
import Tkinter as tk
from PIL import Image, ImageTk
class MainWindow:
def __init__(self, master):
canvas = Canvas(master)
canvas.pack()
self.pimage = Image.open(filename)
self.cimage = ImageTk.PhotoImage(self.pimage)
self.image = canvas.create_image(0,0,image=self.cimage)
filename = full_filename
root = tk.Tk()
x = MainWindow(root)
mainloop()
and I get the following error:
TclError: image "pyimage36" doesn't exist
I've read some stuff about the image objects getting garbage cleaned but I don't quite understand it.
Figured it out. For some reason, while running in the debugger, if any previous executions had thrown errors I get the "pyimage doesn't exist" error. However, if I restart the debugger (or no previously executed scripts have thrown errors), then the program runs fine.
I had the same error message when using spyder 3.3.6 the only way i could get the .png file to load and display after getting the 'Tinker pyimage error ' was to go to the Console and restart the kernel. After that i worked fine.
(Python 3.8)
If you are using a IDE with a console(such as Spyder) just call root.mainloop() in the console.
Odds are that you have a bunch of partially loaded tkinter GUI's that never managed to be executed due to an error that prevented the root.mainloop() function from being run.
Once you have run the root.mainloop() a bunch of GUI's will likely appear on screen. After you have closed all those GUI's try running your code again.
Image of multiple Tkinter GUI's appearing on screen
Read more about mainloop() here: https://pythonguides.com/python-tkinter-mainloop/
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
img = Image.open('Paste the directory path')
bg = ImageTk.PhotoImage(img)
lbl = Label(root, image=bg)
lbl.place(x=0, y=0)
mainloop()
I was getting the same error. Try this code this will help you.
Additionally, in case if you create a button and use it to open other window, then there use window = Toplevel(), otherwise it will again show the same error.
From programmersought
image “pyimage1” doesn’t exist
Because there can only be one root window in a program, that is, only one Tk() can exist, other windows can only exist in the form of a top-level window (Toplevel()).
Original code
import tkinter as tk
window = tk.TK()
Revised code
import tkinter as tk
window = tk.Toplevel()
Keep other code unchanged
https://www.programmersought.com/article/87961175215/
I want to know whether I can display an image from the path I have selected? like, I have a path for example: c:\user\desktop\33.jpg, and I want to take only that jpg file and I have to display that image using label or something. If it is possible, I want to know how?
Thanks in advance!
Here is a sample code for what you are asking:
from Tkinter import Label,Tk
from PIL import Image, ImageTk
import tkFileDialog
root = Tk()
path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
im = Image.open(path)
tkimage = ImageTk.PhotoImage(im)
myvar=Label(root,image = tkimage)
myvar.image = tkimage
myvar.pack()
root.mainloop()
You will be wanting to add a button for calling the askopenfilename because right now its calling it the moment the program begins.
Also you might wanna add more file extensions to filetypes
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.
I'm writing a program and I need to inform the user about some changes with a popup message, but not a popup window. Something like the rectangle informing about new message in Kadu - no window, just a bitmap drawn directly on the screen for a few seconds.
I wonder if there is a simple way to do that with win32 package or Tkinter, and handle the event when the user clicks on the rectangle.
Actually the message would be constant, so the bitmap might be loaded from a file, but I still don't know how to start.
Any ideas, please?
Regards, mopsiok
I am using wxPython and looking for a way to have a popup message. Now I am using a popupmenu in which I append each item of the menu with one line of the message.
Actually I found the answer to my question. That's my code using Tkinter, hope it will help you.
from Tkinter import Tk, Label
from Image import open as iopen #doesn't needed if you won't display image
from ImageTk import PhotoImage #as before
root = Tk()
img = PhotoImage(iopen("some_path")) #load an image
label = Label(root, image=img)
label.image = img
label.bind("<Button-1>", Click)
label.pack()
root.geometry('-0-40') #place in the right-bottom corner
root.wm_attributes("-topmost", 1) #popup
root.overrideredirect(1)
root.mainloop()
def Click(event): #close the window if image clicked
root.destroy()
print 'window closed'