I don't know why the image not showing.
Is this a device problem or some update in the Tkinter library?
from tkinter import *
root = Tk()
btn = Button(master=root,image=PhotoImage(file='Sample1.png'))
btn.pack()
root.mainloop()
Sample Image:
When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.
Python garbage-collects any local objects at the end of the scope, even when being used by a Tkinter widget. To prevent this, you need to assign the image to some variable:
photoimage = PhotoImage(file='Sample1.png')
btn = Button(master=root,image=photoimage)
Related: Tkinter vanishing PhotoImage issue
Related
I am trying to create a LabelFrame in tkinter, however the "title" of the frame is not displayed, neither is the border around the LabelFrame.
Minimal example:
import tkinter as tk
root = tk.Tk()
root.title("Test")
root.geometry("400x400")
instance = tk.Label(root, text="SCTL:").pack()
labelframe = tk.LabelFrame(root, text="Title of Frame").pack()
instance2 = tk.Label(labelframe, text="some text").pack(padx=10, pady=10)
root.mainloop()
This example will get "some text" displayed, however "Title of Frame" will not.
I am using Python 3.8.8 and tkinter 8.6.10. Does anybody have an idea how I can get the title of the frame and its border to be displayed?
Thank you in advance!
So the problem here is that you are initializing and packing the labelframe in the same line as #jasonharper pointed out -: labelframe = tk.LabelFrame(root, text="Title of Frame").pack().
Note that this does not work, since the variable labelframe does not get assigned the newly initialized labelframe object but rather the return value of the called function pack.
This means that if we write the same in two different lines, one for object initialization and the other for packing, the problem disappears. Like so -:
labelframe = tk.LabelFrame(root, text="Title of Frame") # First initialize the object and store it in the variable.
labelframe.pack() # Then use the variable to pack it.
In general also if a tkinter widget is to be used for long term in a program I suggest not packing it in the same line as it's initialization, this not only looses the newly initialized object's reference but also can cause such problems.
But the same if is temporary then can be done in one line. Here you were using the LabelFrame in the next line and thus should have separately done the packing and the initialization.
I'm trying to simply add an image to a tkinter button. I tried everything:
import tkinter as tk
root = tk.Tk()
root.geometry('300x300+300+150')
photo = tk.PhotoImage('home.gif')
btn = tk.Button(root, image = photo, width = 100, height = 100)
btn.image = photo # even with this does not work
btn.pack()
root.mainloop()
I also tried with PIL setting the photo variable equal to ImageTk.PhotoImage(Image.open('home.gif')), I tried easly the open function, the absolute path of the photo (and yes, the photo is inside the same directory of my script), but anything works. The window just pop up with a big button, without image inside.
UPDATE:
I tried with other images, and I noticed that some images are shown while others no. This is because the images with transparent background cause a bug or a problem to tkinter... so, I do not know if there's a way to solve this. On google I find out that some people use canvas but I actually need the image to be inside the button so I do not know how to do.
Please change your code as below
photo = tk.PhotoImage(file='home.gif')
because i changed the above code and it worked....
I am using Python 2.7 and want to load a .gif logo on the Tkinter frame but there is a problem that it open two windows all the time (one empty and one with logo).
Codes:
import Tkinter
root = Toplevel()
logo = PhotoImage(file="D:\\.....\\....\\****.gif")
w1 = Label(root, compound = CENTER, image = logo).pack(side="right")
root.mainloop()
how can I have only one window with my logo?
Every tkinter app needs a Tk() window a.k.a root for other widgets to exist. If you don't create it explicitly, it will be created implicitly. Your empty window is that implicitly created Tk() window and the other one is Toplevel() you created.
So you need to change this line
root = Toplevel()
to
root = Tk()
Additionally, please keep the reference of your image.
When you add a PhotoImage or other Image object to a Tkinter widget,
you must keep your own reference to the image object. If you don’t,
the image won’t always show up.
The problem is that the Tkinter/Tk interface doesn’t handle references
to Image objects properly; the Tk widget will hold a reference to the
internal object, but Tkinter does not. When Python’s garbage collector
discards the Tkinter object, Tkinter tells Tk to release the image.
But since the image is in use by a widget, Tk doesn’t destroy it. Not
completely. It just blanks the image, making it completely
transparent…
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().
I'm trying to make a script which will enable me to dynamically update an image object and then post the updated image to a Tkinter Canvas widget. The code here is prototype code, just to get the basics down. The aim here is to put a blue pixel on the image being displayed by the canvas, at the click location.
Something very strange is going on here. I'm using the Wing IDE, and if I run this code through the debugger, with a breakpoint at any line in the woohoo function, and then continue execution after hitting the breakpoint, the code works exactly as expected- putting a blue pixel on the image. If I run the code normally, or through the debugger with no breakpoints, the image is never updated. This leads me to the conclusion that there is some internal wizardry going on which I haven't got much hope of understanding without aid.
I'd really like to know the best way to go about this (or any way, I guess), and if someone could explain to me what's going on under the hood that'd be really cool. Thanks.
from Tkinter import *
from PIL import Image, ImageTk
def woohoo(event):
original.putpixel((event.x,event.y),(0,0,255))
newpic = ImageTk.PhotoImage(original)
c.create_image((0,0),image=newpic, anchor="nw")
main = Tk()
c = Canvas(main, width=300, height=300)
main.geometry("300x300+0+0")
c.pack()
original = Image.open("asc.bmp")
picture = ImageTk.PhotoImage(original)
c.create_image((0,0),image=picture, anchor="nw")
c.bind("<Button-1>", woohoo)
main.mainloop()
My guess is, you're creating a new image in a function. The reference to the image is a local variable. When the function exits, the reference is garbage collected which causes the new image to be destroyed. Most likely, running interactively causes the garbage collector to run differently (perhaps more lazily?)
Changed a little of the other post to work with Python 3+ :
from tkinter import *
def stuff(event):
global picture3
picture3 = PhotoImage(file='picture2.png')
c.itemconfigure(picture2, image = picture3)
main = Tk()
c = Canvas(main, width=300, height=300)
c.pack()
picture = PhotoImage(file='picture1.png')
picture2 = c.create_image(150,150,image=picture)
c.bind("<Button-1>", stuff)
main.mainloop()
try it like this:
from Tkinter import *
from PIL import Image, ImageTk
def woohoo(event):
global picture #
original.putpixel((event.x,event.y),(0,0,255))
picture = ImageTk.PhotoImage(original)#
c.itemconfigure(myimg, image=picture)#
main = Tk()
c = Canvas(main, width=300, height=300)
main.geometry("300x300+0+0")
c.pack()
original = Image.open("asc.bmp")
picture = ImageTk.PhotoImage(original)
myimg = c.create_image((0,0),image=picture, anchor="nw")#
c.bind("<Button-1>", woohoo)
main.mainloop()