Can't make this image to display. I get an error saying TclError: image "pyimage21" doesn't exist
Here is what I tried
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
root.title("Poker ranges")
photo = tk.PhotoImage(file = "C:\\Users\\chaeh\\OneDrive\\Desktop\\poker\\hand_range_grid.png")
text = tk.Label(root,image = photo)
root.mainloop()
You need to pack, grid or place your label...
I suggest to use pathlib to find correct path, because tkinter need relative references to use images...
import pathlib
import tkinter as tk
DIR = pathlib.Path('.')
root = tk.Tk()
root.geometry("500x500")
root.title("Poker ranges")
photo = tk.PhotoImage(file=DIR / "abc.png")
text = tk.Label(root, image=photo)
text.pack()
root.mainloop()
Related
I'm currently learning Tkinter and cannot find a solution to my problem. The Tkinter Label is not showing up in the window and no one has the solution why. I am on MacOS M1 Pro.
from tkinter import *
root = Tk()
# Create label widget
myLabel = Label(root, text="Hello World!")
# Pack it onto the screen
myLabel.pack()
root.mainloop()
Shown Result:
Try adding size to your root window.
from tkinter import *
root = Tk()
root.geometry("500x500")
# Create label widget
myLabel = Label(root, text="Hello World!")
# Pack it onto the screen.
myLabel.pack()
root.mainloop()
I have tried to open this program with double click using python application but it doesn't work.
I test it and I think the problem is the use of ImageTk.
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.title('Images')
root.iconbitmap('G_image.ico')
my_img = ImageTk.PhotoImage(Image.open('2021_1.png'))
my_label = Label(image=my_img)
my_label.pack()
button_quit = Button(root,text='Exit Program',command=root.destroy)
button_quit.pack()
root.mainloop()
Do you have any errors?
But I guess it's because you have to "pack" your label in the root window:
my_label = Label(root, image=img)
my_label.pack()
and to double click on the python file, try this:
right click on the file->open with->choose default program->more options->select python.exe file and click on.
I want to open a new window which includes an image. I don't know how to achieve this. Currently, my code opens up a plain window with a button saying show image.
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.configure(background = "black")
obrazek = ImageTk.PhotoImage(Image.open("dn.jpg"))
def click():
MyLabel = Label(root, image = obrazek)
Tk()
MyLabel.pack()
myButton = Button(root, text = "Wyjście", command = click, fg = "white", bg = "#000000")
myButton.pack()
mainloop()
This is minimal working example.
You have to use Tk() only to create main window. For other windows you have to use Toplevel. And when you have it in variable ie. top then you have to use it as first argument in Label (or other widget) to display widget in this window.
BTW: PEP 8 -- Style Guide for Python Code
#from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
from PIL import ImageTk, Image
# --- functions ---
def click():
top = tk.Toplevel()
my_label = tk.Label(top, image=my_image)
my_label.pack()
# --- main ---
root = tk.Tk()
#root.configure(background="black") # PEP8: without spaces around `=`
my_image = ImageTk.PhotoImage(Image.open("lenna.png")) # PEP8: english names
my_button = tk.Button(root, text="Wyjście", command=click) # PEP8: lower_case_names for variables
my_button.pack()
root.mainloop()
Image from Wikipedia: Lenna
EDIT:
I hope you didn't create PhotoImage inside function because there is bug in PhotoImage which removes image from memory when it is assigned to local variable in function. And then you can see empty image. It has to be assigned to global variable or to some object - ie. my_label.img = image
When I upload an Image in tkinter there is a white frame around the picture. I searched a lot but I didn't foud any answer.
Picture of the white frame:
Here is my code:
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.geometry("1200x583")
root.resizable(0,0)
place_one=Image.open("p1.jpg")
place_one=place_one.resize((1200,583))
place_one_2=ImageTk.PhotoImage(place_one)
place=Label(root,image=place_one_2)
place.place(x=-2,y=0)
character=Image.open("m2.gif")
character=ImageTk.PhotoImage(character)
L1=Label(root,image=character)
L1.place(x=0,y=355)
root.mainloop()
You need to set image border width (borderwidth) ,
L1=Label(root,image=character, borderwidth=0)
Edit:
Do not use import *(why it is bad?) try to,
import tkinter as tk
from PIL import ImageTk,Image
root = tk.Tk()
root.geometry("1200x583")
root.resizable(0,0)
place_one=Image.open("p1.jpg")
place_one=place_one.resize((1200,583))
place_one_2=ImageTk.PhotoImage(place_one)
place=tk.Label(root,image=place_one_2)
0place.place(x=-2,y=0)
character=Image.open("m2.gif")
character=ImageTk.PhotoImage(character)
L1=tk.Label(root,image=character, borderwidth=0)
L1.place(x=0,y=355)
root.mainloop()
from tkinter import *
photo = PhotoImage(file="C:\Temp\test\computer.gif")
lbl = Label(root, image=photo, height="10", width="20").pack
I have absolutely no idea why this won't work it comes up with: _tkinter.TclError: couldn't recognize data in image file "C:\Temp\test\computer.gif.
Windows filenames always have to be entered as raw strings (in all of python, not just with tkinter). Also, you'll have to make a root window first. Try this:
from tkinter import *
root = Tk()
photo = PhotoImage(file=r"C:\Temp\test\computer.gif")
Label(root, image=photo, height="10", width="20").pack()
root.mainloop()