This is the format of code 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 is: Where should I declare the photo Myimage
that I used in my class mycustomwindow
?
If I put Myimage=tk.PhotoImage(data='....')
before the root=tk.Tk()
like below, it will give me the too early to create image
error as we can't create an image before 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 function main()
like this, it says it can't find the image Myimage
in 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 badly wrong with my code structure? Where should I declare Myimage
so that it can be used in class mycustomwindow
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…