Too early to create image at NewWindow - python

I want to display the image in a new window, but I get an error.
This is my error code
photo = PhotoImage(file='img/dog')
File "C:\Users\Hyojae\Anaconda3\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Hyojae\Anaconda3\lib\tkinter\__init__.py", line 3486, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
And this is my code sample.
I would appreciate your help.
from tkinter import *
def messageWindow():
win = Tk()
win.geometry('300x200')
root.destroy()
photo2 = PhotoImage(file="img/dog1.gif")
label1 = Label(win, image=photo2)
label1.grid(row=6)
Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)
win.mainloop()
root = Tk()
photo = PhotoImage(file="img/dog2.gif")
label1 = Label(root, image=photo)
label1.pack()
Button(root, text='Bring up Message', command=messageWindow).pack()
root.mainloop()

You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.
Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.
I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it
from tkinter import *
def messageWindow():
win = Toplevel()
win.geometry('300x200')
root.withdraw() # THIS HIDE THE WINDOW
photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)
Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)
root = Tk()
photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()
B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)
root.mainloop()

Related

Tkinter "couldn't open "pyimage1": no such file or directory" error when trying to open an image

I have been trying to add a background image to my Interface but I always get the error:
"couldn't open "pyimage1": no such file or directory"
Also I am pretty new to python.
Already tried multiple methods with tkinter and PIL aswell as tkinter's canvas which didn't work either
This is the whole programm:
import tkinter as tk
from PIL import Image, ImageTk
class MainMenu:
def __init__(self, master):
#creating Main Frame and Window
master.title("Interface")
#Image
image = Image.open(r"Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
self.background_image = tk.PhotoImage(file=photo)
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image
#Creating Widgets
self.label1 = tk.Label(master, text="Please Enter the text you would
like encrypted: ")
self.entry1 = tk.Text(master, height=5, width=20)
self.button = tk.Button(master, text="Submit", command=self.Submit)
#Adding Widgets to Grid
self.label1.grid(row=0, column=0, padx=5, pady=5)
self.entry1.grid(row=1, column=0, padx=5, pady=5)
self.button.grid(columnspan=2, pady=10)
#Configuration of Widgets and Main window
master.configure(bg="black")
self.button.configure(bg="light blue")
self.label1.configure(bg="black", fg="light blue")
self.entry1.configure(bg="light blue")
def Submit(self):
print("You entered: " + self.entry1.get())
root = tk.Tk()
Mm = MainMenu(root)
root.mainloop()
The main problem would be within these lines I am guessing:
image = Image.open(r"Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
self.background_image = tk.PhotoImage(file=photo)
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image
As you can see I am trying to make an Interface or GUI and everything is working fine except the background image.
Try this:
image = Image.open("Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
#self.background_image = tk.PhotoImage(file=photo) -- Not needed, delete
self.background_label = tk.Label(image=photo)
self.background_label.image = photo
self.background_label.place(x=0,y=0)
#self.background_label.img = self.background_image -- Also not needed, delete
As far as I can tell this simply means you have used 'tk.PhotoImage' twice on a variable.
For example:
item1_image = tk.Label()
image = tk.PhotoImage('image.png')
item1_image.configure(image=tk.PhotoImage(image))
When you're pulling these variables out of different places in a large file, it is hard to keep track of whether 'PhotoImage' is used. I typically use it as early as possible to avoid the image not appearing.

How to make image disappear in tkinter

I've been programming a random operator name generator for Rainbox Six Siege and I want the operators picture to appear when their name comes up. The image appears fine, but it won't go away. This is my Code:
from tkinter import *
import tkinter
import random
names = ['Sledge','Thatcher','Ash','Thermite','Twitch','Montagne','Glaz','Fuze','Blitz','IQ','Buck','Blackbeard','Capitão','Hibana']
name = ["Smoke","Mute","Castle","Pulse","Doc","Rook","Kapkan","Tachanka","Jäger","Bandit","Frost","Valkyrie","Caveira","Echo"]
root = tkinter.Tk()
def pickName():
rad = random.choice(names)
photo = PhotoImage(file=rad+".png")
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
nameLabel.configure(text=rad, foreground="white", background="blue")
root.configure(background='blue')
def pickName1(): nameLabel.configure(text=random.choice(name),background="orange",foreground="black")
root.configure(background='orange')
root.title("Operator Picker")
root.geometry("250x100")
nameLabel = tkinter.Label(root, text="", font=('Helvetica', 32))
nameLabel.pack()
Grid()
f1 = tkinter.Frame(root, height=100, width=100) #defines frame size in
pixels
f1.pack(side=tkinter.LEFT) #packs on the left
f1.pack_propagate(0) #tells frame not to let children control size
pickButton1 = tkinter.Button(f1, command=pickName, text="Pick
Attack",background="blue",foreground="white")
pickButton1.pack(fill=tkinter.BOTH, expand=1) #takes up all available space
f2 = tkinter.Frame(root, height=100, width=100)
f2.pack(side=tkinter.RIGHT)
f2.pack_propagate(0)
pickButton2 = tkinter.Button(f2, command=pickName1, text="Pick
Defend",background="orange",foreground="black")
pickButton2.pack(fill=tkinter.BOTH, expand=1)
root.mainloop()
Note: This is still a WIP, all I need is to know how to get rid of the pictures once they appear. This is what it looks like when more than one image appears: https://imgur.com/eroXLLn
You are adding a new Label every time you call that function. Instead, you should make the Label only once (probably in the initialization stage), and update the picture. Just like you update the text for nameLabel, plus the step to keep the reference.
photo_label = tkinter.Label()
def pickName():
rad = random.choice(names)
photo = PhotoImage(file=rad+".png")
photo_label.configure(image = photo)
photo_label.image = photo # keep a reference!
photo_label.pack()
nameLabel.configure(text=rad, foreground="white", background="blue")
and your whole code should look like:
from tkinter import *
import tkinter
import random
names = ['Sledge','Thatcher','Ash','Thermite','Twitch','Montagne','Glaz','Fuze','Blitz','IQ','Buck','Blackbeard','Capitão','Hibana']
name = ["Smoke","Mute","Castle","Pulse","Doc","Rook","Kapkan","Tachanka","Jäger","Bandit","Frost","Valkyrie","Caveira","Echo"]
root = tkinter.Tk()
photo_label = tkinter.Label()
def pickName():
rad = random.choice(names)
photo = PhotoImage(file=rad+".png")
photo_label.configure(image = photo)
photo_label.image = photo # keep a reference!
photo_label.pack()
nameLabel.configure(text=rad, foreground="white", background="blue")
root.configure(background='blue')
def pickName1(): nameLabel.configure(text=random.choice(name),background="orange",foreground="black")
root.configure(background='orange')
root.title("Operator Picker")
root.geometry("250x100")
nameLabel = tkinter.Label(root, text="", font=('Helvetica', 32))
nameLabel.pack()
Grid()
f1 = tkinter.Frame(root, height=100, width=100) #defines frame size inpixels
f1.pack(side=tkinter.LEFT) #packs on the left
f1.pack_propagate(0) #tells frame not to let children control size
pickButton1 = tkinter.Button(f1, command=pickName, text="PickAttack",background="blue",foreground="white")
pickButton1.pack(fill=tkinter.BOTH, expand=1) #takes up all available space
f2 = tkinter.Frame(root, height=100, width=100)
f2.pack(side=tkinter.RIGHT)
f2.pack_propagate(0)
pickButton2 = tkinter.Button(f2, command=pickName1, text="PickDefend",background="orange",foreground="black")
pickButton2.pack(fill=tkinter.BOTH, expand=1)
root.mainloop()

Tkinter PhotoImage won't Work

I have been creating a file that displays a image on a canvas. I created the 'PhotoImage' file that I use to store my picture.
d = PhotoImage(file="pic.gif")
canvas = Canvas(root, 500, 500)
pic = canvas.create_image(20, 20, image=d)
But I just produce an error each time I run the program...for some reason, PhotoImage will never work for me. How do I get this to work?
This is a an example that works for me.
#----------------------------------------------------------------------
import Tkinter
#----------------------------------------------------------------------
root = Tkinter.Tk()
frame = Tkinter.Frame(root)
frame = Tkinter.LabelFrame(root, text="LabelFrame text", padx=5, pady=5)
frame.pack(side=Tkinter.LEFT)
Label1 = Tkinter.Label(frame, text="Label1 text")
Label1.pack()
#----------------------------------------------------------------------
photo1 = Tkinter.PhotoImage(file = 'Chart_Example.gif')
#
width_1 = photo1.width()
height_1 = photo1.height()
#
x_center_1 = width_1 / 2.0
y_center_1 = height_1 / 2.0
#---------------------------------
iframe1 = Tkinter.Frame(frame, bd=2, relief=Tkinter.RAISED)
iframe1.pack(expand=1, fill=Tkinter.X, pady=5, padx=5, side=Tkinter.LEFT)
c1 = Tkinter.Canvas(iframe1, width=width_1, height=height_1)
c1.create_image(x_center_1, y_center_1, image=photo1, anchor = Tkinter.CENTER)
c1.pack(side=Tkinter.LEFT)
#----------------------------------------------------------------------
root.mainloop()
#----------------------------------------------------------------------

(Tkinter) Image won't show up in new window

I just started using python tkinter and I have a button that opens a new window. One the new window there is an image, but the image won't show up.Can you please help me solve my problem?
from tkinter import *
def nwindow():
nwin = Toplevel()
nwin.title("New Window")
btn.config(state = 'disable')
photo2 = PhotoImage(file = 'funny.gif')
lbl2 = Label(nwin, image = photo2)
lbl2.pack()
def quit():
nwin.destroy()
btn.config(state = 'normal')
qbtn = Button(nwin, text = 'Quit', command = quit)
qbtn.pack()
main = Tk()
main.title("Main Window")
main.geometry("750x750")
photo = PhotoImage(file = 'funny.gif')
lbl = Label(main, image = photo)
lbl.pack()
btn = Button(main, text = "New Winodw", command = nwindow)
btn.pack()
main.mainloop()
your coding doesn't work but putting .mainloop() should fix your issue
def nwindow():
nwin = Toplevel()
nwin.title("New Window")
btn.config(state = 'disable')
photo2 = PhotoImage(file = 'funny.gif')
lbl2 = Label(nwin, image = photo2)
lbl2.pack()
nwin.mainloop()

Tkinter - How would I create buttons in a new window, which has been created by a function being called? Python 3

from tkinter import *
def begin():
root = Tk()
root.title("main window")
root.geometry("1920x1080")
return #How would a button be placed in this window made by this function?
root = Tk()
root.title("Start up page")
root.geometry("1920x1080")
BeginButton = Button(app, text = "Begin", command=begin, bg="green")
BeginButton.grid(column = 2, row = 2, sticky = W)
BeginButton.config(height = 10, width = 30 )
root.mainloop()
How would I create new buttons in the new window, if the new window is being made by, in this case a function known as "begin".
Any response would be much appreciated!
I believe what you want to do is to modify the root window rather than create a new one. Here is a minimal working example:
from tkinter import *
root = Tk()
class App:
def __init__(self):
root.title("Start up page")
root.geometry("1920x1080")
self.beginB = Button(root, text="Begin", command=self.begin,
bg="green", height=10, width=30)
self.beginB.grid(sticky = W)
def begin(self):
root.title("main window")
self.beginB.destroy()
del self.beginB
self.goB = Button(root, text='Go on', command=self.go_on,
bg='red')
self.goB.grid(sticky=E)
def go_on(self):
self.label = Label(root, text="you have continued")
self.label.grid(row=1, sticky=S)
App()
root.mainloop()
An advantage of defining a class is that you can make forward references. In your code, you had to define the begin function before you create the begin button. With a class, I could put it after init, which to me is a more natural order. It is ofter the case that the initialization code calls or binds more than one function.

Categories

Resources