-1

I'm trying to make a project for my GitHub page and to learn how to code. I have problem with Tkinter, when I try to add some background photo nothing change.

class GuiImainPage:
    def __init__(self):
        self.root = Tk()
        self.lib = Library()
        self.MAIN_PAGE()

    def MAIN_PAGE(self):
        self.root.title('Library')
        Label(self.root, text="Choose Login Or Register", width="300", height="2").pack()
        Label(self.root, text="").pack()
        Button(self.root, text="Login", height="2", width="30", command=self.login_window).pack()
        Button(self.root, text="Register", height="2", width="30", command=self.register_windows).pack()
        self.CONFIGURE_GUI(self.root, None, None)
        self.SET_IMAGE()
        self.root.mainloop()

    def SET_IMAGE(self):
        my_canvas = tk.Canvas(self.root, width=width, height=height)
        my_canvas.pack(fill=tk.BOTH, expand=tk.YES)
        bg = tk.PhotoImage(file="bookImage.png")
        my_canvas.create_image(10, 10, image=bg,anchor="nw")

GuiImainPage()

Where CONFIGURE is some regular settings for all my pages. I think that should be enough to resolve the problem (Now file have ~400 lines).

If it is possible I want to not use canvas (If it require to change all file).

I try many combinations where SET_IMAGE should be invoke.

Ben the Coder
  • 539
  • 2
  • 5
  • 21

1 Answers1

1

The background image is being defined as a local variable in the function SET_IMAGE, so the data of the image is being garbage collected after the function ends. Therefore, TkInter cannot draw the image because the data for the image is now blank.

To fix this, simply define the image as an instance variable instead. This will cause it not to be garbage collected:

        self.bg = tk.PhotoImage(file="bookImage.png")

Also, if you want to use the image as a background image, then using a canvas is probably not the way to go.

It is simpler to put the image in a tk.Label and then use the place method to make the label fill the whole window:

    def SET_IMAGE(self):
        self.bg = tk.PhotoImage(file="bookImage.png")
        self.bg_label = tk.Label(self.root, image=self.bg)
        self.bg_label.place(x=0, y=0, relwidth=1, relheight=1)

The relwidth and relheight arguments will cause the image to fill the whole window.

Also, to make the background image actually appear at the back (and not cover everything else), make sure you call SET_IMAGE before packing the other widgets:

    def MAIN_PAGE(self):
        self.root.title('Library')
        self.SET_IMAGE()
        Label(self.root, text="Choose Login Or Register", width="300", height="2").pack()
        Label(self.root, text="").pack()
        Button(self.root, text="Login", height="2", width="30", command=self.login_window).pack()
        Button(self.root, text="Register", height="2", width="30", command=self.register_windows).pack()
        self.CONFIGURE_GUI(self.root, None, None)
        self.root.mainloop()
Lecdi
  • 2,189
  • 2
  • 6
  • 20
  • oh ok thanks, probably i miss it. – tester tester Apr 08 '23 at 17:22
  • But now image do not fill full window. can i resolve it? – tester tester Apr 08 '23 at 17:22
  • @testertester I will edit my answer now – Lecdi Apr 08 '23 at 17:28
  • Yep that's give me step forward but now every buttons, labels etc disappear :D on main page, another thing is - if i want to have this image on all pages then i should every time invoke SET_IMAGE? @Lecdi – tester tester Apr 08 '23 at 17:37
  • @testertester I will edit my answer to help with the fact that all the buttons are disappearing. For the question about pages, I cannot help you, because you have not attached all of your code in the question so I don't know how you are programming the different pages. – Lecdi Apr 08 '23 at 17:41
  • 1
    Thanks you, I move it before you comment and it's work. You are life saver, thanks @Lecdi – tester tester Apr 08 '23 at 17:45