I'm programing a background to this program and it gives a second frame when I run it. I dont know why this happens but it does. here is the program
import tkinter as tk
import os
from PIL import ImageTk, Image
parent = os.path.dirname(os.path.realpath(__file__))
assets = os.path.join(parent,"assets")
backgrounds = os.path.join(assets,"backgrounds")
print(assets)
root = tk.Toplevel()
frame = tk.Frame(root)
frame.pack()
path = os.path.join(backgrounds,"red to blue.png")
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
root.mainloop()
Ok so the answer was that I needed it to be tk.Tk() instead of tk.Toplevel() and it works now.
Related
I'm writing a code and I'm getting this error:
this is all the nessacery code
import os
import random
from PIL import ImageTk, Image
import tkinter as tk
def controlmenu():
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
path = "C:\\Users\user\\Documents\\Codes\\Python\\beathouse\\images\\controllermapping.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
root.mainloop()
root = tk.Tk()
frame = tk.Frame(root)
root.config(bg="black")
root.title("menu")
frame.pack()
bgc = "black"
fgc = "white"
conrtols=tk.Button(frame,
fg=fgc,
bg=bgc,
text="view controls",
command=controlmenu)
conrtols.pack(side=tk.LEFT)
root.mainloop()
and this is the error
this is not the exact code but i tested this exactly and it threw this error
meaning there are the same errors
Credit to Cool Cloud.
So the code says:
root=tk.Tk()
Well it needs to say:
root=tk.TopLevel()
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def save():
editor = Tk()
img= Image.open('images/save.png')
fixed= ImageTk.PhotoImage(img)
btn_save_editor = Button(editor, image=fixed)
btn_save_editor.place(x=30, y=80, height=20)
root.mainloop()
From another interface, if you mean another window, then you should use a TopLevel widget, not TK
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def save():
editor = Toplevel()
editor.geometry("500x500") # you can change it
img = Image.open('images/save.png')
fixed= ImageTk.PhotoImage(img)
btn_save_editor = Button(editor, image=fixed)
btn_save_editor.place(x=30, y=80, height=20)
save()
root.mainloop()
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 5 years ago.
I have the following code:
from tkinter import *
import os
from PIL import ImageTk, Image
#Python3 version of PIL is Pillow
class Scope:
def __init__(self, master):
self.master = master
f = Frame(root)
self.greet_button = Button(f, text="Back", command=self.back)
self.greet_button.pack(side= LEFT)
self.greet_button = Button(f, text="Detect", command=self.detect)
self.greet_button.pack(side= LEFT)
self.close_button = Button(f, text="Continue", command=master.quit)
self.close_button.pack(side=LEFT)
photo = PhotoImage(file='demo.gif')
cv = Label(master, image=photo)
cv.pack(side= BOTTOM)
f.pack()
def greet(self):
print("Previous image...")
root = Tk()
my_gui = Scope(root)
root.mainloop()
My first problem is that when I run this, all the buttons and the window show up, but there is no image. There is a square place holder indicating that the image should be in that box, but no image actually shows. I'm able to display the image if I just type the following:
root = Tk()
photo = PhotoImage(file='demo.gif')
label = Label(root, image=photo)
label.pack()
root.mainloop()
So, I know it's possible. But I don't know what I'm doing wrong with my GUI code. I've tried debugging this quite a bit and nothing seemed to work.
A second problem is, I am completely unable to display a jpg file in a GUI. I've tried using every tutorial and nothing quite does the trick. Ideally, I'd like to just be able to display a jpg image, if that's not possible, I'll settle for displaying a gif.
Your reference to photo gets destroyed / carbage collected by Python after the class is called, so, there is nothing the label could show.
In order to avoid this, you have to maintain a steady reference to it, i.e. by naming it self.photo:
from tkinter import *
import os
from PIL import ImageTk, Image
#Python3 version of PIL is Pillow
class Scope:
def __init__(self, master):
self.master = master
f = Frame(root)
self.greet_button = Button(f, text="Back") #, command=self.back)
self.greet_button.pack(side=LEFT)
self.greet_button = Button(f, text="Detect") #, command=self.detect)
self.greet_button.pack(side=LEFT)
self.close_button = Button(f, text="Continue", command=master.quit)
self.close_button.pack(side=LEFT)
self.photo = PhotoImage(file='demo.gif')
cv = Label(master, image=self.photo)
cv.pack(side=BOTTOM)
f.pack()
def greet(self):
print("Previous image...")
root = Tk()
my_gui = Scope(root)
root.mainloop()
PS: Your code snippet was not running properly because two functions were missing.
I've been working to get a simple Tkinter canvas to display an image using create_image. I've read many threads that say that you need to create a reference to the object outside any function or class, otherwise the image object will be garbage collected. Unfortunately, I still cannot get this to work. Below is my code as it stands. Ignore all the colors - I use them to illustrate where the frames and canvas live on the window.
-Kirk
import Tkinter as tk
from PIL import Image
from PIL import ImageTk
imageList = []
image = Image.open('len_std.jpg')
#event handlers
def hit_sel_click():
imageList = []
test_image = ImageTk.PhotoImage(image)
imageList.append(cnv_hits.create_image(0,0,
image=test_image))
#start root
root = tk.Tk()
root.title('SimView')
root.resizable(width=False, height=False)
#target/control variables
hit_sel = tk.StringVar() #holds radio button with activity level
#build GUI
frm_hits = tk.Frame(root, height=800, width=200, bg='#FF0000')
frm_hits.grid(rowspan=3, sticky=tk.W+tk.N+tk.S+tk.E)
tk.Label(frm_hits, text='Activity:').grid()
tk.Radiobutton(frm_hits, text='Low', variable=hit_sel, value='Low',
command=hit_sel_click).grid(sticky=tk.W)
tk.Radiobutton(frm_hits, text='Medium', variable=hit_sel, value='Medium',
command=hit_sel_click).grid(sticky=tk.W)
tmp = tk.Radiobutton(frm_hits, text='High', variable=hit_sel,value='High',
command=hit_sel_click)
tmp.grid(sticky=tk.W)
tmp.select()
frm_hit_list = tk.Frame(frm_hits, bg='#002288')
frm_hit_list.grid(sticky=tk.W+tk.N+tk.E+tk.S)
scrl_hits = tk.Scrollbar(frm_hit_list, orient=tk.VERTICAL)
scrl_hits.grid(row=0, column=1, sticky=tk.N+tk.S)
cnv_hits = tk.Canvas(frm_hit_list, bg='#888800',width=200, height=200,
yscrollcommand=scrl_hits.set)
cnv_hits.grid(row=0, column=0, sticky=tk.W+tk.N+tk.E+tk.S)
scrl_hits.config(command=cnv_hits.yview)
root.mainloop()
You are using test_image to draw the image of cnv_hits. That is right, but you forgot that test_image is local to hit_sel_click() method; which thing means it is not available to your main program.
To resolve this, you have 2 choices:
Either declare test_image as global inside hit_sel_click()
Or run test_image = ImageTk.PhotoImage(image) before you declare hit_sel_click().
Nota Bene:
For the first case, you will need to run root = tk.Tk() before hit_sel_click().
In case you choose the second option, you will need to run root = tk.Tk() before test_image = ImageTk.PhotoImage(image)
If you don't do this, your program will raise a RuntimeError exception.
I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself.
Currently, I can display an image like so:
import Tkinter as tk
import ImageTk
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()
However, when the user hits, say the ENTER key, I'd like to change the image.
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
# change image
root.bind("<Return>", callback)
root.mainloop()
Is this possible?
The method label.configure does work in panel.configure(image=img).
What I forgot to do was include the panel.image=img, to prevent garbage collection from deleting the image.
The following is the new version:
import Tkinter as tk
import ImageTk
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
img2 = ImageTk.PhotoImage(Image.open(path2))
panel.configure(image=img2)
panel.image = img2
root.bind("<Return>", callback)
root.mainloop()
The original code works because the image is stored in the global variable img.
Another option to do it.
Using object-oriented programming and with an interactive interface to update the image.
from Tkinter import *
import tkFileDialog
from tkFileDialog import askdirectory
from PIL import Image
class GUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
w,h = 650, 650
master.minsize(width=w, height=h)
master.maxsize(width=w, height=h)
self.pack()
self.file = Button(self, text='Browse', command=self.choose)
self.choose = Label(self, text="Choose file").pack()
self.image = PhotoImage(file='cualitativa.gif')
self.label = Label(image=self.image)
self.file.pack()
self.label.pack()
def choose(self):
ifile = tkFileDialog.askopenfile(parent=self,mode='rb',title='Choose a file')
path = ifile.name
self.image2 = PhotoImage(file=path)
self.label.configure(image=self.image2)
self.label.image=self.image2
root = Tk()
app = GUI(master=root)
app.mainloop()
root.destroy()
Replace 'cualitativa.jpg' for the default image you want to use.
Another solution that might be of help.
In my case, I had two tk.Tk() windows. When using ImageTk.PhotoImage(), the object defaults to setting its tk window to being the first one created. A simple fix to this is to pass the tk window that you want as such ImageTk.PhotoImage(img, master=your_window)
import tkinter as tk
from PIL import ImageTk, Image
if __name__ == '__main__':
main_window = tk.Tk()
second_window = tk.Tk()
main_canvas = Canvas(second_window)
main_canvas.pack()
filename = 'test.png'
img = Image.open(filename)
img = img.resize((300, 100), Image.ANTIALIAS)
logo = ImageTk.PhotoImage(img, master=second_window)
logo_label = Label(master=main_canvas, image=logo)
logo_label.image = logo
logo_label.pack()
main_window.mainloop()