Delrius Euphoria
Delrius Euphoria

Reputation: 15098

Tkinter image labels disappearing when new toplevel opened

I have a root window with the image labels and then i have a manage toplevel opening(with the same image and variable inside of a function), when it opens the images in the root is no more there and the image is displayed at the new toplevel only. Could it be cuz im using same variable? i have tried changing variable names but no luck :( Note that im using Pmw here for the Balloon widget(tooltips) could the issue be related to it, though i dont think so

Here is a code ive reduced maximum:

on the root window:

# Creating ? icons
q_mark = Image.open('Image/question_mark.png')
q_mark_re = q_mark.resize((15, 15), Image.ANTIALIAS)
q_mark_new = ImageTk.PhotoImage(q_mark_re)

# Making 2 ? icons
q_mark_1 = Label(root, image=q_mark_new)
q_mark_1.grid(row=0, column=2, padx=(0, 10))
q_mark_2 = Label(root, image=q_mark_new)
q_mark_2.grid(row=1, column=2, padx=(0, 10))

# Creating a tooltip for each ? icon
nametooltip_1 = Pmw.Balloon(root)
nametooltip_1.bind(q_mark_1, 'Name:\nEnter a valid full name')
nametooltip_2 = Pmw.Balloon(root)
nametooltip_2.bind(q_mark_2, 'Phone Number:\nEnter a phone number less than 11 digits')

on the manage toplevel:

def manage():
    global q_mark_new

    # Defining Login window
    admin = Toplevel(root)
    admin.title('Login')
    admin.focus_force()
.....
    # Creating ? icons
    q_mark = Image.open('Image/question_mark.png')
    q_mark_re = q_mark.resize((15, 15), Image.ANTIALIAS)
    q_mark_new = ImageTk.PhotoImage(q_mark_re)

    # Making 13 ? icons
    q_mark_1 = Label(admin, image=q_mark_new)
    q_mark_2 = Label(admin, image=q_mark_new)
    q_mark_1.grid(row=2, column=1, padx=(5, 130))
    q_mark_2.grid(row=4, column=1, padx=(5, 130))

    nametooltip_1 = Pmw.Balloon(root)
    nametooltip_2 = Pmw.Balloon(root)
    nametooltip_1.bind(q_mark_1, 'Username:\nEnter the given username')
    nametooltip_2.bind(q_mark_1, 'Password:\nEnter the given correct password')

Now i get the image on the manange window but not anymore on the root window

Before(notice the q_mark on the side of entry boxes):

enter image description here

After(notice its gone in the root window and it came up in the manage window:

enter image description here

Thanks in advance :D

Upvotes: 0

Views: 482

Answers (1)

OysterShucker
OysterShucker

Reputation: 5531

Assign a name to your original creation of q_mark and do not create it again

q_mark = Image.open('Image/question_mark.png')
q_mark_re = q_mark.resize((15, 15), Image.ANTIALIAS)
q_mark_new = ImageTk.PhotoImage(q_mark_re, name="qmark")

Use the name everywhere you want to use the image

def manage():
    #don't need this
    #global q_mark_new

    # Defining Login window
    admin = Toplevel(root)
    admin.title('Login')
    admin.focus_force()
.....
    # Creating ? icons ~ DON'T DO THIS
    #q_mark = Image.open('Image/question_mark.png')
    #q_mark_re = q_mark.resize((15, 15), Image.ANTIALIAS)
    #q_mark_new = ImageTk.PhotoImage(q_mark_re)

    # Making 13 ? icons
    q_mark_1 = Label(admin, image="qmark")
    q_mark_2 = Label(admin, image="qmark")
    q_mark_1.grid(row=2, column=1, padx=(5, 130))
    q_mark_2.grid(row=4, column=1, padx=(5, 130))

    nametooltip_1 = Pmw.Balloon(root)
    nametooltip_2 = Pmw.Balloon(root)
    nametooltip_1.bind(q_mark_1, 'Username:\nEnter the given username')
    nametooltip_2.bind(q_mark_1, 'Password:\nEnter the given correct password')

Upvotes: 1

Related Questions