如何在tkinter中使用图像

时间:2015-10-31 20:30:01

标签: image user-interface python-3.x tkinter

我想在我的窗口制作一个横幅,以便在顶部。我如何导入图像?如果它有帮助,这是我的代码。另外作为旁注,我删除了我试过的代码。我尝试将图像放入我的代码存储的文件中,然后尝试访问它。这是错误的做法吗?我知道这里有一些例子,但它们对我不起作用所以我发布了我的代码以获得更直接的回复

import tkinter

#create new window
window = tkinter.Tk()
#name window
window.title("Basic window")
#window sized
window.geometry("250x200")
#creates label then uses ut
lbl = tkinter.Label(window, text="Please sign up or log in ", bg="green")
#pack label
lbl.pack()
#create username
lbl_username = tkinter.Label(window, text="Username", bg="green")
ent_username = tkinter.Entry(window)
#pack username
lbl_username.pack()
ent_username.pack()
#attempting to get the ent_username info to store
#configure window
window.configure(background="red")
#basic enter for password
lbl_password = tkinter.Label(window, text="Password", bg="green")
ent_password = tkinter.Entry(window)
#pack password
lbl_password.pack()
ent_password.pack()
#def to check if username is valid
def question():
    username = ent_username.get()
    password = ent_password.get()
    if username == "louis":
        print("you know")
    else:
        print("failed")
    if password == "2xfeikis":
        print("it passed for pass")
    else:
        print("it failed for pass")

def sign_up():
    username = ent_username.get()
    password = ent_password.get()
    if len(username) > 0 and len(password) > 0:
        pass
    else:
        print("invalid info")
#will make the sign up button and will call question on click
btn = tkinter.Button(window, text="Log", command=lambda: question(), bg ="grey")
#sign up button
btn_sign_up = tkinter.Button(window, text="Sign up", command= lambda: sign_up(), bg ="grey")
#pack buttons
btn.pack()
btn_sign_up.pack()
#draw window
window.mainloop()

1 个答案:

答案 0 :(得分:0)

使用照片图像

以下是文档http://effbot.org/tkinterbook/photoimage.htm