【发布时间】:2013-07-19 14:40:50
【问题描述】:
这是我目前拥有的代码格式:
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()
我的问题是:我应该在哪里声明我在课堂上使用的照片Myimage mycustomwindow?
如果我将Myimage=tk.PhotoImage(data='....') 放在root=tk.Tk() 之前,如下所示,它会给我too early to create image 错误,因为我们无法在根窗口之前创建图像。
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()
如果我像这样将Myimage=tk.PhotoImage(data='....') 放在函数main() 中,它表示在class mycustomwindow 中找不到图像Myimage。
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()
我的代码结构有什么严重问题吗?我应该在哪里声明 Myimage 以便它可以在 class mycustomwindow 中使用?
【问题讨论】: