This question has already been answered:

I learned how to place images on the main window in my program, but now I want to know how to place an image on a child window.

I put all the code on the file exchanger as the code is long.

Here is a sample code:

mGui = Tk() ment = StringVar() mGui.protocol("WM_DELETE_WINDOW", callback) mGui.geometry('450x450+500+300') mGui.title('R6S Operator Randomizer | Closed Beta 5.0') w1 = Label(text='Copyright © 2018 JMax. All rights reserved').place(x = 212, y = 430, width = 250) img = ImageTk.PhotoImage(Image.open("Sledge.jpg")) panel = Label(mGui, image = img) panel.pack(side = "bottom", fill = "both", expand = "no") 

Here is the child window:

 def wpronostico(): Child() class Child(tk.Toplevel): def __init__(self): super().__init__(mGui) self.init_child() def scelta(self): tk.Label(self, text='' + random.choice(code_list3)).place(x = 87, y = 110) def init_child(self): self.title('Pronostico') self.geometry('200x200') tk.Button(self, text='Vincerai?', command=self.scelta).pack() 

All code .

Reported as a duplicate by jfs python Jan 20 '18 at 4:14 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    "The link to the image should be saved" because Upon completion of the function, the picture is deleted in CPython, since there are no more references to it as commented on @jfs , so most likely you did not create an image on the child window when you tried to implement it. To do this, save the variable with the image as shown in the example (i.e., called self.img )

    An example if you only need a child window image.

     from tkinter import * from PIL import ImageTk, Image class Main(Tk): def __init__(self): super().__init__() self.geometry('250x250+500+300') self.title('Info') button_1 = Button(self, text='открыть окно', font='Times 12', command=self.started_create) button_1.place(x=0, y=0) def started_create(self): self.top_level = Top() class Top(Toplevel): def __init__(self): super().__init__() self.title('открытое окно') self.geometry('400x400') self.img = ImageTk.PhotoImage(Image.open("Sledge.jpg")) self.panel = Label(self, image=self.img) self.panel.pack(side="bottom", fill="both", expand="no") if __name__ == "__main__": main = Main() main.mainloop() 

    enter image description here


    And this example gives you the opportunity to use your image in both the main window and the child window.

     from tkinter import * from PIL import ImageTk, Image class Main(Tk): def __init__(self): super().__init__() self.geometry('250x250+500+300') self.title('Info') self.img = ImageTk.PhotoImage(Image.open("Sledge.jpg")) self.panel_1 = Label(self, image=self.img) self.panel_1.pack(side="bottom", fill="both", expand="no") button_1 = Button(self, text='открыть окно', font='Times 12', command=self.started_create) button_1.place(x=0, y=0) def started_create(self): self.top_level = Top() self.top_level.panel['image'] = self.img class Top(Toplevel): def __init__(self): super().__init__() self.title('открытое окно') self.geometry('400x400') self.panel = Label(self) self.panel.pack(side="bottom", fill="both", expand="no") if __name__ == "__main__": main = Main() main.mainloop() 

    or so following your previous question :

     from tkinter import * from PIL import Image, ImageTk def info_window(): windows = Toplevel() windows.title('открытое окно') windows.geometry('400x400') panel = Label(windows, image=img) panel.pack(side="bottom", fill="both", expand="no") button = Button(windows, text='открыть окно', font='Times 12', command=infowindow) button.place(x=0, y=0) mGui1 = Tk() mGui1.geometry('250x250+500+300') mGui1.title('Info') img = ImageTk.PhotoImage(Image.open("Sledge.jpg")) panel_main = Label(mGui1, image=img) panel_main.pack(side="bottom", fill="both", expand="no") button_1 = Button(mGui1, text='открыть окно', font='Times 12', command=info_window) button_1.place(x=0, y=0) mGui1.mainloop() 

    enter image description here