-2

It comes up with no coding errors. It just doesn't show the image.

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        image = tkinter.PhotoImage(file="NORTHERNICE (1).gif")
        #../Documents/
        tkinter.Label(self, image=image).pack()
        start = Button(self, text ="Choose a class", fg="#a1dbcd", bg="#383a39", command = lambda: controller.show_frame('PageOne'), font=("Gisha", 10))
        start.pack()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Zayn Ahmed
  • 11
  • 1
  • the StartPage(tk.Frame): is in the code, before the define block which is indented – Zayn Ahmed Nov 23 '16 at 13:43
  • Assuming that `__init__` is a constructor from a class you should include the class name and the code where the class is instantiated. – gus27 Nov 23 '16 at 14:01
  • umm im sort of new how would i do this? a friend helped me with this code – Zayn Ahmed Nov 23 '16 at 14:03
  • What I meant is that you should show the parts of your code in your question which include the class name (e.g. `class MyClass:`) and the instantiation of the class (e.g. `def test = MyClass()`). – gus27 Nov 23 '16 at 14:07
  • ill upload all of the code in a sec – Zayn Ahmed Nov 23 '16 at 15:55

1 Answers1

0

Tkinter is a bit of an unusual beast, because there are two languages involved here - Python (in which you write your code), and Tcl (which hosts the Tk user interface toolkit). There are some oddities in the interaction between the two environments, and you've run into perhaps the most glaring of them. Specifically, there is no proper synchronization between the lifetime of an actual PhotoImage object (which lives entirely on the Tcl side), and the Python PhotoImage object which acts as a proxy to it. Currently, if the Python object is ever garbage-collected, the Tcl object is automatically deleted, even though there might still be a Tcl-side reference to the image. The consequence of this is that you CANNOT use only a local variable (like image in your code) for the Python reference to the image, as it will go away at the end of the function. Storing the image in a global variable would work, but might give the image too long of a lifetime (it would never be deleted, even if the window using it was closed). Storing it as an instance variable would work (self.image instead of image, in your case), as the Python instance will presumably be around exactly as long as the Tcl widget it describes. Another popular option is to store the image as an attribute of the widget that uses the image.

jasonharper
  • 9,450
  • 2
  • 18
  • 42