0
    from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image

win=tkinter.Toplevel()

wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625) 
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625) 

ent8=StringVar()


def code():
    btn1.destroy()
    add=StringVar()
    sub=StringVar()
    pro=StringVar()

    img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
    Label2= Label(wrapper2, image=img)
    Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')

    def Find():
        add.set(float(ent00.get())+float(ent01.get()))
        sub.set(float(ent00.get())-float(ent01.get()))
        pro.set(float(ent00.get())*float(ent01.get()))

    ent00=Entry(wrapper, width=15)
    ent00.grid(row=4, column=1, padx=10, pady=10, sticky='w')
    ent01=Entry(wrapper, width=15)
    ent01.grid(row=5, column=1, padx=10, pady=10, sticky='w')

    lbl8=Label(wrapper, text="Add", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=10, sticky='w')
    ent8=Entry(wrapper, textvariable=add, width=15, state='readonly')
    ent8.grid(row=6, column=1, padx=10, pady=10, sticky='w')

    lbl15=Label(wrapper, text="Subtract", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=20, pady=10, sticky='w')
    ent15=Entry(wrapper, textvariable=sub, width=15, state='readonly')
    ent15.grid(row=7, column=1, padx=10, pady=10, sticky='w')

    lbl9=Label(wrapper, text="Product", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=8, column=0, padx=20, pady=10, sticky='w')
    ent9=Entry(wrapper, textvariable=pro, width=15, state='readonly')
    ent9.grid(row=8, column=1, padx=10, pady=10, sticky='w')

    btn = Button(wrapper, text = 'Calculate', command=Find, bd = '5', width=15, height=2)
    btn.grid(row=11, column=1, padx=20, pady=10)

def img():  
    if ent8.get()=="4":
        img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
        Label2= Label(wrapper3, image=img)
        Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
        print("Move ahead")
    else:
        print("Try again")

btn2 = Button(wrapper, text = 'Image', command=img, bd = '5', width=15, height=2)
btn2.grid(row=12, column=1, padx=20, pady=10)        

btn1 = Button(wrapper, text = 'OPEN CODE', command=code, bd = '5', width=20, height=2)
btn1.grid(row=11, column=1, padx=20, pady=10)

win.geometry("1400x700+250+250")
win.mainloop()

Two images need to be shown on the tkinter. The one defined earlier in wrapper2, shows empty frame while the one that has to appear in wrapper3 after getting 4 as sum, does not appear at all. Moreover, the output printed is "Try again". Why it is so? When sum is 4 it has to show "Move ahead".enter image description here

Ricky
  • 69
  • 1
  • 7
  • You have to keep a reference to your `ImageTk.PhotoImage` objects. Basically make sure you can always access it. Try putting it in a global list. – TheLizzard May 19 '21 at 10:19
  • I am not from coding background....so not getting u......hence can you adjust and post the code. – Ricky May 19 '21 at 11:20

2 Answers2

3

First of all, terrible names.
Both your function and your PhotoImage are named img. Rename the function to def add_img(). Second, looking at your code I have no idea what all the wrapper frames are for, why not name them according to what they are planned to hold? Same applies to all the widgets. Wouldn't calc_btn be a better name than btn? img_btn instead of btn2? Why do you need to read more than the name to know what something is?

Third, you have ent8 twice in your code. Once as Label and again as a StringVar.

Tkinter constantly refreshes your window so you need to save the image you are using.
Personally I would have done all of this in a class.

For right now, with your current code, just add
loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg")) before your functions and instead of using the variables you are using to open the image, just use Label(wrapper3, image=loaded_img)

As in:

win = Toplevel()

wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)

ent8=StringVar()

loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg"))

Edit
Here is the entire code:

from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image

win=Toplevel()

wrapper=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper.place(x=0, y=80, width=465, height=625)
wrapper3=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper3.place(x=950, y=80, width=465, height=625)
wrapper3_title=Label(wrapper3, text="Selected Data", bg="crimson", fg="white", font=("times new roman",30,"bold"))
wrapper3_title.grid(row=0,column=0,padx=20, pady=10)
wrapper2=Frame(win, bd=4, relief=RIDGE, bg="crimson")
wrapper2.place(x=465, y=80, width=485, height=625)

ent8=StringVar()

loaded_img = ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
add_strvar = StringVar()
sub_strvar = StringVar()
pro_strvar = StringVar()

def code():
    btn1.destroy()

    Label2= Label(wrapper2, image=loaded_img)
    Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')

    def Find():
        add_strvar.set(float(ent00.get())+float(ent01.get()))
        sub_strvar.set(float(ent00.get())-float(ent01.get()))
        pro_strvar.set(float(ent00.get())*float(ent01.get()))

    ent00=Entry(wrapper, width=15)
    ent00.grid(row=4, column=1, padx=10, pady=10, sticky='w')
    ent01=Entry(wrapper, width=15)
    ent01.grid(row=5, column=1, padx=10, pady=10, sticky='w')

    lbl8=Label(wrapper, text="Add", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=10, sticky='w')
    ent8=Entry(wrapper, textvariable=add_strvar, width=15, state='readonly')
    ent8.grid(row=6, column=1, padx=10, pady=10, sticky='w')

    lbl15=Label(wrapper, text="Subtract", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=20, pady=10, sticky='w')
    ent15=Entry(wrapper, textvariable=sub_strvar, width=15, state='readonly')
    ent15.grid(row=7, column=1, padx=10, pady=10, sticky='w')

    lbl9=Label(wrapper, text="Product", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=8, column=0, padx=20, pady=10, sticky='w')
    ent9=Entry(wrapper, textvariable=pro_strvar, width=15, state='readonly')
    ent9.grid(row=8, column=1, padx=10, pady=10, sticky='w')

    btn = Button(wrapper, text = 'Calculate', command=Find, bd = '5', width=15, height=2)
    btn.grid(row=11, column=1, padx=20, pady=10)


def add_img():
    if add_strvar.get() == "4.0":
        Label2= Label(wrapper3, image=loaded_img)
        Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
        print("Move ahead")
    else:
        print("Try again")

btn2 = Button(wrapper, text = 'Image', command=add_img, bd = '5', width=15, height=2)
btn2.grid(row=12, column=1, padx=20, pady=10)

btn1 = Button(wrapper, text = 'OPEN CODE', command=code, bd = '5', width=20, height=2)
btn1.grid(row=11, column=1, padx=20, pady=10)

win.geometry("1400x700+250+250")
win.mainloop()

Edit 2
Code changed to work with classes:

from tkinter import *
from tkinter import ttk
from PIL import ImageTk ,Image


class ImageCalculator:
    def __init__(self, img_path):
        self.window = Toplevel()
        self.window.geometry("1400x700+250+250")
        self.mainframe = Frame(self.window)
        self.mainframe.pack(expand=True, fill=BOTH)
        self.bg_color = 'crimson'

        frame_settings = {'master': self.mainframe, 'bd': 4,
                          'relief': RIDGE, 'bg': self.bg_color}
        frame_names = ('left', 'center', 'right')
        self.frames = {name: Frame(**frame_settings) for name in frame_names}

        frame_height = 625
        init_y = 80
        frame_widths = {'left': 465, 'center': 485, 'right': 465}
        x = 0
        for name in frame_names:
            frame_width = frame_widths[name]
            self.frames[name].place(x=x, y=init_y, width=frame_width,
                                    height=frame_height)
            x += frame_width
        self.setup_right_wrapper()
        self.code_btn = self.setup_left_wrapper()

        self.loaded_image = ImageTk.PhotoImage(Image.open(img_path))

        self.add_strvar = StringVar()
        self.sub_strvar = StringVar()
        self.pro_strvar = StringVar()

    def setup_left_wrapper(self) -> Button:
        code_btn = Button(self.frames['left'], text='OPEN CODE', command=self.code,
                          bd='5', width=20, height=2)
        img_btn = Button(self.frames['left'], text='Image', bd='5', width=15,
                         height=2, command=self.add_img)

        code_btn.grid(row=11, column=1, padx=20, pady=10)
        img_btn.grid(row=12, column=1, padx=20, pady=10)

        return code_btn

    def setup_right_wrapper(self):
        right_frame_title = Label(self.frames['right'], text="Selected Data",
                                  bg=self.bg_color, fg="white",
                                  font=("times new roman",30,"bold"))
        right_frame_title.grid(row=0, column=0, padx=20, pady=10)

    def code(self):
        def Find():
            self.add_strvar.set(float(first_entry.get())
                                + float(second_entry.get()))
            self.sub_strvar.set(float(first_entry.get())
                                - float(second_entry.get()))
            self.pro_strvar.set(float(first_entry.get())
                                * float(second_entry.get()))

        self.code_btn.destroy()

        Label2 = Label(self.frames['center'], image=self.loaded_image)
        Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')

        left_frame = self.frames['left']
        first_entry = Entry(left_frame, width=15)
        second_entry = Entry(left_frame, width=15)

        # Settings of all labels
        lbl_settings = {'bg': self.bg_color, 'fg': 'white',
                        'font': ("times new roman", 15, "bold")}
        # Setting of all entry.
        entry_settings = {'width': 15, 'state': 'readonly'}

        add_lbl = Label(left_frame, text="Add", **lbl_settings)
        add_entry = Entry(left_frame, textvariable=self.add_strvar,
                          **entry_settings)

        sub_lbl = Label(left_frame, text="Subtract", **lbl_settings)
        sub_entry = Entry(left_frame, textvariable=self.sub_strvar,
                          **entry_settings)

        pro_lbl = Label(left_frame, text="Product", **lbl_settings)
        pro_entry = Entry(left_frame, textvariable=self.pro_strvar,
                          **entry_settings)

        calc_btn = Button(left_frame, text='Calculate', command=Find, bd='5',
                          width=15, height=2)

        # Widget placement.
        first_entry.grid(row=4, column=1, padx=10, pady=10, sticky='w')
        second_entry.grid(row=5, column=1, padx=10, pady=10, sticky='w')
        add_lbl.grid(row=6, column=0, padx=20, pady=10, sticky='w')
        add_entry.grid(row=6, column=1, padx=10, pady=10, sticky='w')
        sub_lbl.grid(row=7, column=0, padx=20, pady=10, sticky='w')
        sub_entry.grid(row=7, column=1, padx=10, pady=10, sticky='w')
        pro_lbl.grid(row=8, column=0, padx=20, pady=10, sticky='w')
        pro_entry.grid(row=8, column=1, padx=10, pady=10, sticky='w')
        calc_btn.grid(row=11, column=1, padx=20, pady=10)

    def add_img(self):
        if self.add_strvar.get() == "4.0":
            Label2 = Label(self.frames['right'], image=self.loaded_image)
            Label2.grid(row=0, column=2, padx=10, pady=5, sticky='w')
            print("Move ahead")
        else:
            print("Try again")


def main():
    img_calc = ImageCalculator('Amritsar.jpg')

    mainloop()


if __name__ == "__main__":
    main()
BoobyTrap
  • 967
  • 7
  • 18
  • 1
    def add_img(): if ent8.get()=="4.0": Label2= Label(wrapper3, image=loaded_img) Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w') print("Move ahead") else: print("Try again") I have done as you recommended. Although the image in wrapper 2 is appearing but the one in this function is not coming and output is still Try Again instead of Move ahead – Ricky May 19 '21 at 12:09
  • @KomalpreetSingh The reason the output is `Try Again` is because `ent8` is never 4 or 4.0 (or anything else in that matter). You never set a value for `ent8`, the `StringVar`. Either pull the 3 `StringVar` variables that you are using out of the `code` function (place `add`, `sub` and `pro` below the `loaded_img`) or look into working with classes, would be much cleaner. After you pulled the 3 `StringVar` out of `code`, change `emt8.get()` into `add.get()`. – BoobyTrap May 19 '21 at 12:17
  • I did but still not working......can u post the code as u r saying...that would be really helpful – Ricky May 19 '21 at 12:25
  • @KomalpreetSingh I have edited again and added a version with a class and different improvements, check it out, it might help you out in the future. – BoobyTrap May 19 '21 at 13:16
0

After setting an image for a Label, you need to keep a reference to this image. Otherwise, it's removed at the end of the function and you lose the image (it's garbage collected).

So, when you define your label, just add a line that stores the image as an attribute of the label:

    img=ImageTk.PhotoImage(Image.open("Amritsar.jpg"))
    Label2= Label(wrapper2, image=img)
    Label2.grid(row=0, column=0, padx=10, pady=5, sticky='w')
    Label2.dontloseit = img

The Label2 widget now has a nonsensical attribute called .dontloseit which holds the image. Now, it won't get collected and it will show in your tkinter widget.

It's one of the peculiarities of tkinter.

Martin Wettstein
  • 2,771
  • 2
  • 9
  • 15
  • That isn't going to work. `Label2` also goes out of scope so it will be garbage collected. If `Label2` is garbage collected, `Label2.dontloseit` will also be garbage collected. Also this is technically a python/PIL peculiarity. – TheLizzard May 19 '21 at 11:26