0

The problem with this code is that , i know the actual .png file isnt corrupted , I've tested it multiple times in other projects , im not sure why my photo1 image isnt appearing inside of my Toplevel window. Any Reason why this is occuring in my toplevel widget and how to fix it?

from tkinter import *
from tkinter import ttk


root = Tk()
#placing of the window
w = 600
h = 250
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws/2) - (w/2)
y =  (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w,h,x , y))
#Window title 
root.title(string=  "Longwood Panther Math Game Main Menu")
#The window configuration 
root.resizable(width = False , height = False)
#level variable 








def language_Arts(a):
  root.withdraw()
  language_window = Toplevel()
  language_window.iconbitmap('panther.ico')
  #title
  language_window.title(string = 'Language Arts Main Menu')
  #placing of the window
  w = 500
  h = 250
  ws = language_window.winfo_screenwidth()
  hs = language_window.winfo_screenheight()
  x = (ws/2) - (w/2)
  y =  (hs/2) - (h/2)
  language_window.geometry('%dx%d+%d+%d' % (w,h,x , y))
  #Greetings
  Greetings =Message(language_window , text = 'Welcome to The Language Arts Section, this is Where you Can Brush up on Your Grammar , and improve gramatical errors. With Multiple levels based on your skill level(grade classification)'  , font= 'times 10 bold')
  Greetings.grid(row = 2 , column = 0 , sticky = 'we') 
  #elementary
  elementary = ttk.Button(language_window , text = 'Elementary')
  elementary.grid(row  = 2 , column = 1 , sticky = 'we')
  #middle school button
  middleschool = ttk.Button(language_window ,text = 'Middle School')
  middleschool.grid(row = 2 , column  = 2 , sticky = 'we')
  #highschool button
  highschool = ttk.Button(language_window , text = 'High School')
  highschool.grid(row = 2 , column = 3, sticky = 'we')
  # photos
  photo1 = PhotoImage(file = 'grammar-cartoon.png')
  photo2 = PhotoImage(file ='Matt1-240x3005.png')
  #image labels
  image1 = Label(language_window , image = photo1)
  image1.grid(row = 3 , column = 0 , sticky = 'we')



LA_Button = ttk.Button(root , text= 'Language Arts')
LA_Button.grid(row = 4 , column = 4, sticky = 'we')
LA_Button.bind('<Button-1>' , language_Arts)
root.mainloop()
  • You need a _global_ reference to images or they get garbage collected, as in removed from memory. Learn more in [this question](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). `photo1` is a _local_ variable, as in it is only available while under `language_Arts`. You can easily avoid this renaming it to `image1.photo1`. – Nae Dec 10 '17 at 20:19
  • See [this](http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm) for more info. – Nae Dec 10 '17 at 20:26
  • Make sure you first assign `image1` as a label as in `image1 = Label(...)`, _then_ assign `image1.photo1 = PhotoImage...` and then configure image for the label like `image1['image'] = image1.photo1`. – Nae Dec 10 '17 at 20:29
  • @Nae so to get a clear understanding of what you are trying to say , without going to much of research , we have to reference the image/photo of our label a second time?so it doenst get garbage collected by python? – Pha n texProgramming Dec 10 '17 at 20:34
  • Not your label, but your image that is currently held by `photo1` variable. And not twice, but rather only once but as a _global_ variable. I'm not saying you shouldn't research. I'm just saying that I _could_ make the image appear on my end using what I've commented and posts that I've linked with the code you've provided. – Nae Dec 10 '17 at 20:36

0 Answers0