Python Forum
adding backgroung image - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: adding backgroung image (/thread-31757.html)



adding backgroung image - ebolisa - Jan-01-2021

Hi,

The code below brings up 2 windows, Login and IOT.

I'd like to add a background image in the Login window but, for some reasons, the image is added to the Main (parent) window.

Can someone, please, help me to find the error.

TIA

import tkinter as tk

class Main:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)

        # login
        self.login = tk.Button(self.frame, text='Login', command=self.configLogin)

        # iot
        self.iot = tk.Button(self.frame, text='IOT', command=self.configIOT)

        self.iot.pack()
        self.login.pack ( )
        self.frame.pack ( )


    def configLogin( self ):
        self.login = tk.Toplevel(self.master)
        self.login.geometry ( "700x650+100+50" )
        self.login.resizable(False,False)
        self.login.title ( 'Login' )

        # background image
        self.login.bg = tk.PhotoImage ( file = "11.png" )
        self.login.bg_image = tk.Label (image = self.login.bg).place \
                     ( x = 0 , y = 0 , relwidth = 1 , relheight = 1 )
        self.app = Login(self.login)

    def configIOT( self ):
        self.iot = tk.Toplevel(self.master)
        self.iot.geometry ( '350x350' )
        self.iot.resizable(False,False)
        self.iot.title ( 'IOT' )
        self.app = IOT(self.iot)


class Login:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text='Click me', command=lambda:self.DoSomething('Hello'))

        self.frame.pack()
        self.button1.pack()

    def DoSomething( self, val):
        print(val)

class IOT:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text='Click me', command=lambda:self.printSome('Hello'))

        self.frame.pack()
        self.button1.pack()

    def printSome( self, val):
        print(val)

if __name__ == "__main__":
    main = tk.Tk()
    app = Main(main)
    main.geometry('200x200')
    main.mainloop()



RE: adding backgroung image - deanhystad - Jan-01-2021

Where will this label go?
self.login.bg_image = tk.Label (image = self.login.bg)
Since you don't specify a parent my guess is it should be a new window all its own. But I guess tk grabs it and puts it in the root window.


RE: adding backgroung image - ebolisa - Jan-02-2021

(Jan-01-2021, 09:33 PM)deanhystad Wrote: Where will this label go?
self.login.bg_image = tk.Label (image = self.login.bg)
Since you don't specify a parent my guess is it should be a new window all its own. But I guess tk grabs it and puts it in the root window.

It's my understanding that whatever is included in the def configLogin(self) should apply to the login window.


RE: adding backgroung image - deanhystad - Jan-02-2021

When you make the label that displays the image you are not specifying a parent for who gets the label. You need something like this:
self.login.bg_image = tk.Label (self.login, image = self.login.bg)  # <- Need to specify a parent for the label
self.login.bg_image.place( x = 0 , y = 0 , relwidth = 1 , relheight = 1 )
Notice I split up making and placing the label. place() returns None, so if you want a handle to the bg image you cannot create and place the image in a single command.


RE: adding backgroung image - JudyLarose - Jan-08-2021

Actually I am having the same problem and I just want to say that it solved mine problem. That was about my address of background.

find a scrolltest and how it works and use of it...