Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

class - Python Tkinter PhotoImage

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It does not matter much where you declare the image, as long as

  1. you create it after initializing Tk() (the problem in your first approach)
  2. the image is in the variable scope when you are using it (the problem in your second approach)
  3. the image object does not get garbage-collected (another common pitfall)

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()

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()

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...