This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 4 years ago.
How come I can't add images using this:
from tkinter import *
root = Tk()
def logo():
photo = PhotoImage(file="Logo.png")
Label(root, image=photo).grid()
logo()
root.mainloop()
But I can add images using this:
from tkinter import *
root = Tk()
photo = PhotoImage(file="Logo.png")
Label(window, image=photo).grid()
logo()
root.mainloop()
Any help?
You have to keep a reference to the image to prevent it being garbage collected. Try this:
def logo():
photo = PhotoImage(file="Logo.png")
lbl = Label(root, image=photo)
lbl.image = photo # keep a reference
lbl.grid()
You don't have to do that in your other block because you are using global variables, which are never garbage collected.
See the note on the bottom of this page.
Related
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 6 months ago.
I am trying to make a sprite sheet editor.
I have a button where you can select an image.
When I put the code in a function it doesn't work, but when it is outside a function it does work.
The BrowseComputer function is what I am having a problem with
from tkinter import *
from tkinter import filedialog
#Window specs
window = Tk()
window.title("TileSplit")
window.configure(background="white")
filepath = ""
#Searches computer and displays Image
def BrowseCompter():
filepath = filedialog.askopenfilename()
photo = PhotoImage(file = filepath)
print(filepath)
SelectedImage = Label(window, image=photo, bg="White").grid(row=0,column=0)
#Makes a variable with information about the instruction text on the screen, Sets the text background, Text color, font and location
InstructionsLabel = Label(window, text="Please select the image you would like to split:" ,bg="white" ,font= "none 12 bold")
EmptyLabel = Label(window, text="" ,bg="white" ,font= "none 12 bold")
#Makes a button
ImportImageButton = Button(window, text="Browse:", command=BrowseCompter)
#Location data
InstructionsLabel.grid(row=0, column=60, sticky=E)
ImportImageButton.grid(row=3,column=0)
EmptyLabel.grid(row=1, column=0)
#Tells python to make the window
window.mainloop()
I think you have a typo. The function shown is actually named 'BrowseCompter'.
also you might need to change your command in the button from 'BrowseCompter' to 'BrowseCompter()'
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 9 months ago.
I am trying to display an image in a window with tkinter, by clicking a button.
I created this function :
def tabuim():
tabu = Frame(win, width=600, height=400)
tabu.pack()
tabu.place(anchor='center', relx=0.8, rely=0.35)
img2 = ImageTk.PhotoImage(Image.open("Tabu.jpg"), master=win)
label2 = Label(tabu, image = img2, borderwidth=3, relief="solid")
label2.pack()
with this button :
button2 = Button(win, text = 'Tabu Route Solution', command = tabuim)
button2.place(relx = 0.5, rely = 0.5)
But it partially works. It only show a border without my image...
Thanks for you help
The following example displays a background image in a frame:
Import tkinter
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
Create a photoimage object of the image in the path
image1 = Image.open("<path/image_name>")
test = ImageTk.PhotoImage(image1)
label1 = tkinter.Label(image=test)
label1.image = test
Position image
label1.place(x=<x_coordinate>, y=<y_coordinate>)
root.mainloop()
This an example you can do as instead of resizing before loading the page you could resize after the image is loaded.
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 10 months ago.
I was trying to show the image when I clicked the button however it failed( the program does not report an error but the image is still not displayed). I'm sure I put the right path to the picture. This is code
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
def def_btn1():
image1 = Image.open("csdl.png")
image1 = image1.resize((710, 400), Image.ANTIALIAS)
test = ImageTk.PhotoImage(image1)
lbl2 = tk.Label(image=test)
lbl2.pack()
btn1 = tk.Button(text="click to show", relief=tk.RIDGE, width=15, font=(12),command=def_btn1)
btn1.pack()
window = tk.mainloop()
I want when I click the button the image will show in the program. Thank you!
You need to add lbl2.image = test for the image to not be destroyed by tkinter, you new code should be this -
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk() # You might want to move this line below your functions, with the other global variables
def def_btn1():
image1 = Image.open("csdl.png")
image1 = image1.resize((710, 400), Image.ANTIALIAS)
test = ImageTk.PhotoImage(image1)
lbl2 = tk.Label(image=test)
lbl2.image = test # You need to have this line for it to work, otherwise it is getting rid of the image. the varibale after the '=' must be the same as the one calling "ImageTk.PhotoImage"
lbl2.pack()
btn1 = tk.Button(text="click to show", relief=tk.RIDGE, width=15, font=(12),command= lambda : def_btn1()) # adding 'lambda :' stops the function from running straight away, it will only run if the button is pressed
btn1.pack()
window = tk.mainloop()
This question already has answers here:
How to reconfigure tkinter canvas items?
(2 answers)
Closed 1 year ago.
While learning Tkinter, I got a problem while creating a function for a button.
I need to config a canvas text in function. But I don't know how to do it.
canvas.create_text(700,350,font=("Arial Bold", 35),fill='white')
def clicked():
res = "THANKYOU " + txt.get()
res1.config(text=res)
I want that res1 to be that canvas.create_text()
You can make use of canvas.itemconfig/canvas.itemconfigure to change the properties of items on canvas.
minimal example:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, bg="black")
canvas.pack()
text = canvas.create_text((100,50),text="Hello", fill='white')
def clicked():
res = "THANKYOU "
canvas.itemconfig(text, text=res)
tk.Button(root, text="click", command=clicked).pack()
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.