【问题标题】:Can I use a picture as a icon on a button in Toplevel ()?我可以在Toplevel()中使用图片作为按钮上的图标吗?
【发布时间】:2018-04-24 06:12:53
【问题描述】:

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

【问题讨论】:

  • 请向我们展示一个 minimal reproducible example 来说明问题,而不是仅仅给我们一个模糊的代码描述。
  • 其实我是python的初学者。我试图在学生数据库上建立一个小项目。所以起初我成功地构建了没有用户 ID 和密码的项目。但现在我想添加一个登录页面。这就是我将主数据库 Tk() 窗口移动到 Toplevel() 并将登录窗口添加为 Tk() 的原因。但是添加后,登录后数据库窗口中没有出现图片图标。
  • 在评论中更详细地描述您的代码无济于事。编辑问题以包含 minimal reproducible example 否则没有人可以帮助您。
  • 代码有点大。这就是为什么我不能在这里给。
  • 好的,我会努力的。

标签: python tkinter toplevel


【解决方案1】:

如果我知道您想在Toplevel 窗口中的button 上显示image。图像是在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()

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 2012-09-30
    • 2022-07-02
    • 2022-01-19
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多