title: Python Tkinter PhotoImage
body: <p>This is the format of code I currently have:</p>
  
  <pre><code>import Tkinter as tk
  
  class mycustomwidow:
      def __init__(self,parent,......)
          ......
          ......
          tk.Label(parent,image=Myimage)
          tk.pack(side='top')
  
  def main():
      root=tk.Tk()
      mycustomwindow(root)
      root.mainlopp()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>My problem is: Where should I declare the photo <code>Myimage</code> that I used in my class <code>mycustomwindow</code>?</p>
  
  <p>If I put <code>Myimage=tk.PhotoImage(data='....')</code> before the <code>root=tk.Tk()</code> like below, it will give me the <code>too early to create image</code> error as we can't create an image before the root window.</p>
  
  <pre><code>import Tkinter as tk
  Myimage=tk.PhotoImage(data='....') 
  class mycustomwidow:
      def __init__(self,parent,......)
          ......
          ......
          tk.Label(parent,image=Myimage)
          tk.pack(side='top')
  
  def main():
      root=tk.Tk()
      mycustomwindow(root)
      root.mainlopp()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>If I put <code>Myimage=tk.PhotoImage(data='....')</code> in the function <code>main()</code> like this, it says it can't find the image <code>Myimage</code> in <code>class mycustomwindow</code>.</p>
  
  <pre><code>import Tkinter as tk
  
  class mycustomwidow:
      def __init__(self,parent,......)
          ......
          ......
          tk.Label(parent,image=Myimage)
          tk.pack(side='top')
  
  def main():
      root=tk.Tk()
      Myimage=tk.PhotoImage(data='....')
      mycustomwindow(root)
      root.mainlopp()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Is there anything badly wrong with my code structure? Where should I declare <code>Myimage</code> so that it can be used in <code>class mycustomwindow</code>?</p>