0

I'm a begginner programming in Python. I'm using Tkinter library to create a window with a drop down menu. I created a Label for show a country flag when the item is selected on the drop down menu. But somethings is going wrong, nothing happens. Here is my code, I'll be so thankful for who helps me how to procced and fix it.

from tkinter import *

root = Tk()
root.geometry('500x450+450+150')
root.title('Python Program')

menu = Menu(root)
root.config(menu=menu)

def callFlag():
    photo = PhotoImage('image.png')
    flag = Label(root, text='teste', image=photo)
    flag.pack()

subMenu = Menu(menu, tearoff=0)
menu.add_cascade(label='Bandeiras', menu=subMenu)
subMenu.add_command(label='Mongólia', command=callFlag)
subMenu.add_command(label='Opção 2', command=callFlag)

subMenu2 = Menu(menu, tearoff=0)
menu.add_cascade(label='Menu 2', menu=subMenu2)
subMenu2.add_command(label='Opção 1', command=callFlag)
subMenu2.add_command(label='Opção 2', command=callFlag)
subMenu2.add_separator()
subMenu2.add_command(label='Exit', command=exit)

root.mainloop()
Lucas Ribeiro
  • 103
  • 1
  • 8

1 Answers1

0

Does this work for you?

from tkinter import *

root = Tk()
root.geometry('500x450+450+150')
root.title('Python Program')

menu = Menu(root)
root.config(menu=menu)

def callFlag():
    photo = PhotoImage(file='image.png')
    flag = Label(root, text='teste', image=photo)
    flag.image = photo
    flag.pack()


subMenu = Menu(menu, tearoff=0)
menu.add_cascade(label='Bandeiras', menu=subMenu)
subMenu.add_command(label='Mongólia', command=callFlag)
subMenu.add_command(label='Opção 2', command=callFlag)

subMenu2 = Menu(menu, tearoff=0)
menu.add_cascade(label='Menu 2', menu=subMenu2)
subMenu2.add_command(label='Opção 1', command=callFlag)
subMenu2.add_command(label='Opção 2', command=callFlag)
subMenu2.add_separator()
subMenu2.add_command(label='Exit', command=exit)

root.mainloop()

The catch seems to be avoiding Python's garbage collection as the function exits by keeping an extra reference to photo (flag.image = photo).

Alternatively, you can the define the image and the flag along with everything else, and pass the flag's pack() function in add_command:

from tkinter import *

root = Tk()
root.geometry('500x450+450+150')
root.title('Python Program')

menu = Menu(root)
root.config(menu=menu)

photo = PhotoImage(file='image.png')
flag = Label(root, text='teste', image=photo)

subMenu = Menu(menu, tearoff=0)
menu.add_cascade(label='Bandeiras', menu=subMenu)
subMenu.add_command(label='Mongólia', command=flag.pack)
subMenu.add_command(label='Opção 2', command=flag.pack)

subMenu2 = Menu(menu, tearoff=0)
menu.add_cascade(label='Menu 2', menu=subMenu2)
subMenu2.add_command(label='Opção 1', command=flag.pack)
subMenu2.add_command(label='Opção 2', command=flag.pack)
subMenu2.add_separator()
subMenu2.add_command(label='Exit', command=exit)

root.mainloop()
Pantalaimon
  • 49
  • 1
  • 7
  • Is has worked to me thank you! How about to clean the Label and appear another flag and select another one? I tried to use "flag = Label(root, image=photo)" outside from the "def" but it says the variable "photo" cannot be founded – Lucas Ribeiro Nov 26 '18 at 18:02
  • That's a different question. I think at this point you're better off using `ImageTk` (see https://stackoverflow.com/questions/3482081/how-to-update-the-image-of-a-tkinter-label-widget) – Pantalaimon Nov 26 '18 at 18:38