Python Forum
adding backgroung image
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding backgroung image
#1
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()
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 adding image thewolf 3 3,223 Feb-21-2021, 08:28 PM
Last Post: thewolf
  Adding an image to a tkinter window djwilson0495 2 8,002 Aug-23-2020, 11:07 AM
Last Post: ebolisa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020