0

yes i do know how to configure the background image however in this case i do not know what to reference as the parent of the widget as nothing is appearing when i use self.

For an example of the code i will be using the boiler plate from Bryan Oakleys switching frames code:

import tkinter as tk                
from tkinter import font  as tkfont 


class SampleApp(tk.Tk):

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

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        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 = {}
        for F in (StartPage, PageOne):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

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

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

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

        **### Here for example this does nto do anything to the window**

        self.background_image= tk.PhotoImage("image_path")
        self.background_label = tk.Label(self, image=self.background_image)
        self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
        self.background_label.image = self.background_image


class PageOne(tk.Frame):

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


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

Thanks in advance for any help!

Moller Rodrigues
  • 780
  • 5
  • 16
  • `, relwidth=1, relheight=1` ? thats small .... beside that ... those are local variables that vanish as soon as you leave `__init__` ... maybe you ment to use `self.background_label` to let them stay around a little longer? If I were a python interpreter I would probably destroy those variables as of yet - they are no longer needed – Patrick Artner Feb 09 '19 at 18:05
  • Oh srry i plucked that code from another post but despite that there is still no image appearing – Moller Rodrigues Feb 09 '19 at 18:10
  • You need to keep a reference to the image. Add background_label.img = background_image after your place method – Henry Yik Feb 09 '19 at 18:11
  • @HenryYik Ive tried that aswell still no image – Moller Rodrigues Feb 09 '19 at 18:14
  • See https://stackoverflow.com/a/10181434/7505395 ... _"Note: if you are doing this inside a function, make sure you keep a reference to the image, otherwise the image will be destroyed by the garbage collector when the function returns. A common technique is to add a reference as an attribute of the label object:"_ ... also read http://effbot.org/tkinterbook/label.htm – Patrick Artner Feb 09 '19 at 18:15
  • Ive implemented your solution but still not producing an image – Moller Rodrigues Feb 09 '19 at 18:18
  • 1
    @PatrickArtner: a `relwidth` of 1 isn't small. Relative widths go between 0 and 1, so one actually means it is as wide as its container. – Bryan Oakley Feb 10 '19 at 04:56

1 Answers1

2

Well I just noticed your syntax for PhotoImage is also wrong. You can read it up here.

self.background_image= tk.PhotoImage("image_path")

Should be:

self.background_image= tk.PhotoImage(file="image_path")

Also add self.background_label.img = self.background_image and then you are all set.

Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • Ah basically the thing i was missing was the self reference i juss forgot the file parameter as i extracted it from my main app just to make a sample one for this question – Moller Rodrigues Feb 09 '19 at 18:23