This is the code format that I currently have:
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()
My problem: where should I declare the Myimage photo that I used in mycustomwindow class?
If I put Myimage=tk.PhotoImage(data='....') before root=tk.Tk() , as shown below, it will give me an error too early to create image , since we cannot create an image in front of the root window .
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()
If I put Myimage=tk.PhotoImage(data='....') in the main() function like this, it says that it cannot find the Myimage image in the class mycustomwindow .
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()
Is there anything bad in my code structure? Where should I declare Myimage so that it can be used in the class mycustomwindow ?
source share