0

I have started to work with Tkinter recently and stuck here. I want to load an image after clicking a Button. The button will generate a text as well as an image below it.

from tkinter import *
from PIL import ImageTk, Image
main_window = Tk()
main_window.geometry('400x400')
main_window.title("A OR B[enter image description here][1]")

frame = Frame(main_window)


def show():
    if clicked.get() == 'A':
        Label(main_window,text = 'A SELECTED',).pack()
        img = ImageTk.PhotoImage(Image.open('l.png'))
        panel = Label(main_window,image = img)
        panel.pack()
    elif clicked.get() == 'B':
        Label(main_window,text = 'B SELECTED',).pack()
        img = ImageTk.PhotoImage(Image.open('g.png'))
        panel = Label(main_window,image = img)
        panel.pack()   
        
clicked = StringVar()
clicked.set('SELECT A or B ')
drop = OptionMenu(main_window,clicked,*options).pack()    
Button(main_window, text='GENERATE',fg='white',bg='blue',command = show).pack()
main_window.mainloop()

I am getting the text but not the image loaded in tkinter window.

  • 3
    Check this [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – Art Aug 07 '21 at 14:59
  • 2
    Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – Matiiss Aug 07 '21 at 15:19
  • 1
    @Art if you think another question answers current one, you should flag the current question as duplicate – Matiiss Aug 07 '21 at 15:20

0 Answers0