-1

In python, I can use a picture as a icon on a button in Tk() instance. But when I trying to use the picture in case of Toplevel(), it does not work. code:

from tkinter import *
root=Tk()
root.title("Login Window")
root.geometry("300x300")
def mainpage():
    mp=Toplevel()
    mp.title("Main Page")
    new_img=PhotoImage(file="nen.png")
    sea_img=PhotoImage(file="serch.png")
    al_img=PhotoImage(file="shw.png")
    Button(mp,image=new_img).pack(side="top",pady=0)
    Label(mp,text="New Entry",bd=0).pack(side="top",padx=0,pady=0)
    Button(mp,image=sea_img,bd=0,command=srch).pack(side="top")
    Label(mp,text="Serach Record",bd=0).pack(side="top",padx=0,pady=0)
    Button(mp,image=al_img,bd=0,command=al_rcrd).pack(side="top",pady=0)
    Label(mp,text="All Record",bd=0).pack(side="top",padx=0,pady=0)
Label(root,text="User-Id",font=15).grid(row=0,column=0,padx=10,pady=10,sticky=W)
Entry(root).grid(row=0,column=1)
Button(root,text="Login",font=15,command=mainpage).grid(row=1,column=1,padx=10,pady=10)

root.mainloop()  
suraj pal
  • 29
  • 5
  • Please show us a [mcve] that demonstrates the problem, rather than just giving us a vague description of your code. – abarnert Apr 24 '18 at 06:15
  • Actually, I am a beginner of python. I was trying to build a small project on student database. So at first I successfully build the project without user_id and password. But now I want to add a login page. That's why I move the main database Tk() window into Toplevel() and add the login window as a Tk(). But after add, picture icon doesn't appear on the database window after log-in. – suraj pal Apr 24 '18 at 06:26
  • Describing your code in a little more detail in a comment does't help. Edit the question to include a [mcve] or nobody can help you. – abarnert Apr 24 '18 at 06:29
  • The code is little big in size. That's why I can not give here. – suraj pal Apr 24 '18 at 06:31
  • ok I will trying. – suraj pal Apr 24 '18 at 06:32
  • Nobody wants _all_ of your code. That's what the "Minimal" part of "Minimal, Complete, and Verifiable example" means. Please click the link and read the help. – abarnert Apr 24 '18 at 06:32
  • The problem is probably not the fact you're using a `Toplevel`, but that you're using a function. See https://stackoverflow.com/q/16424091/7432 – Bryan Oakley Apr 24 '18 at 12:11

1 Answers1

0

If i understand you want to display image on your button in Toplevel window.The image is cabbage collected in toplevel until you make reference to B.image= new_img it so that it will display in the window. To display on Label on Toplevel you need to save reference to it too.

from tkinter import *


root=Tk()
root.title("Login Window")
root.geometry("300x300")


def mainpage():
    mp=Toplevel()
    mp.title("Main Page")
    new_img=PhotoImage(file="nen.png")
    B = Button(mp, image=new_img)
    B.image= new_img
    B.pack()

Label(root,text="User-Id",font=15).grid(row=0,column=0,padx=10,pady=10,sticky=W)

Entry(root).grid(row=0,column=1)

Button(root,text="Login",font=15,command=mainpage)
grid(row=1,column=1,padx=10,pady=10)

root.mainloop()
AD WAN
  • 1,414
  • 2
  • 15
  • 28