我可以在Toplevel()的按钮上使用图片作为图标吗?

时间:2018-04-24 06:12:53

标签: python tkinter toplevel

在python中,我可以将图片用作Tk()实例中按钮上的图标。但是当我尝试在Toplevel()的情况下使用图片时,它不起作用。 代码:

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()  

1 个答案:

答案 0 :(得分:0)

如果我了解您想在image button窗口中显示Toplevel。图片是toplevel收集的卷心菜,直到您参考B.image= new_img这样它就会显示在窗口中。要在Label Toplevel上显示,您还需要保存对它的引用。

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()
相关问题