0

I spend some hours over the following problem: The picture pic1 (in class StartPage) doesn't show with the following code (but a space of the right size is created for it). When I try outside the class with a particular tk.Tk() object, everything works just fine...). What am I missing?

import tkinter as tk
from tkinter import ttk

LARGE_FONT= ("Verdana", 18)

class MyClass(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = StartPage(container, self)

        self.frames[StartPage] = frame

        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        pic1 = tk.PhotoImage(file="Logo.gif").subsample(15)#.zoom
        label_my_logo = tk.Label(self, image = pic1)
        label_my_logo.pack()

        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)


app = MyClass()
app.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Netchaiev
  • 327
  • 1
  • 2
  • 14
  • Duplicate of *every "Tkinter image doesn't show up" question ever posted*: you have to save an explicit reference to the image or it will be garbage-collected. `label_my_logo.img = pic1`, for example. The `image=` option just saves the *name* of the image, it doesn't count as a reference. – jasonharper Sep 21 '19 at 22:51
  • @jasonharper So is the following code correct? ``` pic1 = tk.PhotoImage(file="Logo.gif").subsample(15) / label_my_logo = tk.Label(self, image = pic1) / label_my_logo.img = pic1/ label_my_logo.pack( )``` (at least it works! ; why outside there is no need to write ```label_my_logo.img = pic1``` – Netchaiev Sep 21 '19 at 23:00
  • Netchaiev: Yes that's correct. Not needed outside a function because there `label_my_logo` would not be a local variable inside a function, it would be global module variable instead and would not be garbage-collected until the whole script ends. – martineau Sep 21 '19 at 23:22

0 Answers0