Python Tkinter PhotoImage [英] Python Tkinter PhotoImage
问题描述
这是目前代码的格式:
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
My problem is: Where should I declare the photo Myimage
that I used in my class mycustomwindow
?
如果我把 Myimage = tk.PhotoImage(data ='....')
在 root = tk.Tk()
之前,如下所示,它会给我太早创建图像
错误,因为我们无法在根窗口之前创建一个图像。
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()
如果我把 Myimage = tk.PhotoImage(data ='.... )在函数
main()
,这样,它说它找不到图像 Myimage
in class mycustomwindow
。
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()
我的代码结构有什么严重错误吗?在哪里应该声明 Myimage
,以便它可以在中使用class mycustomwindow
?
Is there anything badly wrong with my code structure? Where should I declare Myimage
so that it can be used in class mycustomwindow
?
<> 初始化 Tk()
(第一种方法中的问题)
- you create it after initializing
Tk()
(the problem in your first approach) - the image is in the variable scope when you are using it (the problem in your second approach)
- the image object does not get garbage-collected (another common pitfall)
如果你在 main()
方法中定义图像,那么你必须使它全局
If you define the image in your main()
method, then you will have to make it global
class MyCustomWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, parent)
Tkinter.Label(self, image=image).pack()
self.pack(side='top')
def main():
root = Tkinter.Tk()
global image # make image known in global scope
image = Tkinter.PhotoImage(file='image.gif')
MyCustomWindow(root)
root.mainloop()
if __name__ == "__main__":
main()
或者,您可以完全放弃 main()
方法,使其全局自动:
Alternatively, you could drop your main()
method entirely, making it global automatically:
class MyCustomWindow(Tkinter.Frame):
# same as above
root = Tkinter.Tk()
image = Tkinter.PhotoImage(file='image.gif')
MyCustomWindow(root)
root.mainloop()
或者,在 __ init __
方法中声明映像,但确保使用 self
关键字绑定到 Frame
对象,因此当 __ init __
finishes:
Or, declare the image in your __init__
method, but make sure to use the self
keyword to bind it to your Frame
object so it is not garbage collected when __init__
finishes:
class MyCustomWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, parent)
self.image = Tkinter.PhotoImage(file='image.gif')
Tkinter.Label(self, image=self.image).pack()
self.pack(side='top')
def main():
# same as above, but without creating the image
这篇关于Python Tkinter PhotoImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!