0

Okay so I have a class and the main.py is calling the Background class which creates the permeate image. When created outside the class it creates the image when places inside it does not, I am dying please help.

main.py

from tkinter import *
from Classes import *

Master = Tk()
Master.geometry("1024x768")
Master.configure(background = "#000000")

Background(Master)

Master.resizable(False, False)
Master.mainloop()

Classes.py

from tkinter import *

class Background():
  def __init__(self, Master):
    self.Window = Frame(Master)
    self.Window.configure(background = "#000000")
    self.Window.place(x = 0, y = 0, width = 1024, height = 768)

    self.Canvas = Canvas(master = self.Window, bg = "#000000", height = 768, width = 1024, bd = 0, highlightthickness = 0, relief = "ridge")
    self.Canvas.place(x = 0, y = 0)
    
    self.Backgroundimg = PhotoImage(file = f"Background/Background.png")
    self.Background = self.Canvas.create_image(512.0, 384.0, image = self.Backgroundimg)
    
    return
xdeltaxen
  • 11
  • 1
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – jasonharper Nov 12 '22 at 02:35
  • No this is a class not a function close though – xdeltaxen Nov 12 '22 at 02:38
  • So what do you think `def __init__` is, if not a function? – jasonharper Nov 12 '22 at 02:39
  • I think class Background() is a class – xdeltaxen Nov 12 '22 at 02:43
  • 1
    Are you _certain_ the image isn't showing? Or could it be that the instance of `Background` is what's not showing? When I run your code and add enough missing code to make it work, the image appears in the window. Maybe you should make the window resizable, and then resize it to see if it's appearing outside the edges of the window. – Bryan Oakley Nov 12 '22 at 03:17
  • It is because there is no variable referencing the instance of `Background()` and so it is garbage collected. Use `something = Background(Master)` instead. – acw1668 Nov 12 '22 at 15:26
  • it works when I make another class and call that and do self.Background = Background(Master) – xdeltaxen Nov 13 '22 at 00:20
  • \We really ought to have a 'sticky' post about images being garbage-collected – JRiggles Nov 14 '22 at 13:28

0 Answers0