在Tkinter中按下按钮时如何调用函数?

时间:2019-01-30 09:36:37

标签: python tkinter

我已经编写了代码,以便在按下“登录”按钮时可以调用“登录”功能。但是,每次我运行代码并按下按钮时,都会显示错误消息“ _tkinter.TclError:图像“ pyimage2”不存在”,并且我似乎找不到解决方法来修复我的代码。

import tkinter


def Login():
    window = tkinter.Tk()
    window.title("Eat Well")
    window.geometry("295x400")
    UsernameLbl = tkinter.Label(window, text = "Username", fg= "white", bg= "black")
    Utext = tkinter.Entry(window)

    PasswordLbl = tkinter.Label(window, text = "Password", fg = "white", bg= "black")
    Ptext = tkinter.Entry(window, show="*")

    Login = tkinter.Button(window, text = "Login", fg = "black", bg = "honeydew", command = window.destroy  )
    window.configure(background= "#008bb5")
    Photo = tkinter.PhotoImage(file = "Eating.gif")
    w = tkinter.Label(window, image = Photo)




    w.pack()
    UsernameLbl.pack()
    Utext.pack()
    PasswordLbl.pack()
    Ptext.pack()
    Login.pack()
    window.mainloop()

def Mainscreen():
    window = tkinter.Tk()
    window.title("Eat Well")
    window.geometry("295x400")
    Question = tkinter.Label(window, text = "Would you like to create an account or login?", fg = "black", bg = "white")
    Create = tkinter.Button(window, text = "Create an account", fg = "white", bg = "black")
    SignIn = tkinter.Button(window, text =  "Login", fg = "white", bg = "black", command = Login)
    Quit = tkinter.Button(window, text = "Quit", fg = "white", bg = "black", command = window.destroy)
    window.configure(background = "#008bb5")
    Photo = tkinter.PhotoImage(file = "Eating.gif")
    w = tkinter.Label(window, image = Photo)
    w.pack()
    Question.pack()
    Create.pack()
    SignIn.pack()
    Quit.pack()
    window.mainloop()




Mainscreen()

当按下登录按钮时,主屏幕应被破坏,登录屏幕应被打开。但是,当前,只要在主屏幕上按下登录按钮,主屏幕就会保持打开状态,并且登录屏幕显示为空白屏幕。

2 个答案:

答案 0 :(得分:2)

这应该有效。注意

的使用
`tkinter.Toplevel()

Image.open。这是因为调用该函数的按钮本身位于活动窗口中。

import tkinter
from PIL import Image, ImageTk


def Login():
    window = tkinter.Toplevel()
    window.title("Eat Well")
    window.geometry("295x400")
    UsernameLbl = tkinter.Label(window, text = "Username", fg= "white", bg= "black")
    Utext = tkinter.Entry(window)

    PasswordLbl = tkinter.Label(window, text = "Password", fg = "white", bg= "black")
    Ptext = tkinter.Entry(window, show="*")

    Login = tkinter.Button(window, text = "Login", fg = "black", bg = "honeydew", command = window.destroy  )
    window.configure(background= "#008bb5")
    im = Image.open("Eating.gif")
    Photo = ImageTk.PhotoImage(im)
    w = tkinter.Label(window)
    w.pack()
    UsernameLbl.pack()
    Utext.pack()
    PasswordLbl.pack()
    Ptext.pack()
    Login.pack()
    window.mainloop()

def Mainscreen():
    window = tkinter.Tk()
    window.title("Eat Well")
    window.geometry("295x400")
    Question = tkinter.Label(window, text = "Would you like to create an account or login?", fg = "black", bg = "white")
    Create = tkinter.Button(window, text = "Create an account", fg = "white", bg = "black")
    SignIn = tkinter.Button(window, text =  "Login", fg = "white", bg = "black", command = Login)
    Quit = tkinter.Button(window, text = "Quit", fg = "white", bg = "black", command = window.destroy)
    window.configure(background = "#008bb5")
    im = Image.open("Eating.gif")
    Photo = ImageTk.PhotoImage(im)
    w = tkinter.Label(window)
    w.pack()
    Question.pack()
    Create.pack()
    SignIn.pack()
    Quit.pack()
    window.mainloop()

答案 1 :(得分:1)

好的,问题是您试图同时运行两个Tk()实例,但您不应该这样做。描述了source codehere

的原因

您可以使用window = tkinter.Tk()来代替Login()中的window = tkinter.Toplevel()来解决以下问题:

import tkinter

def Login():
    # window = tkinter.Tk()
    window = tkinter.Toplevel()
    window.title("Eat Well")
    window.geometry("295x400")
    user_name_label = tkinter.Label(window, text="Username", fg="white", bg="black")
    user_name_text = tkinter.Entry(window)

    password_label = tkinter.Label(window, text="Password", fg="white", bg="black")
    password_text = tkinter.Entry(window, show="*")

    login = tkinter.Button(window, text="Login", fg="black", bg="honeydew", command=window.destroy)
    window.configure(background="#008bb5")
    photo = tkinter.PhotoImage(file="Eating.gif")
    w = tkinter.Label(window, image=photo)

    w.pack()
    user_name_label.pack()
    user_name_text.pack()
    password_label.pack()
    password_text.pack()
    login.pack()
    window.mainloop()

def Mainscreen():
    window = tkinter.Tk()
    window.title("Eat Well")
    window.geometry("295x400")
    question = tkinter.Label(window, text="Would you like to create an account or login?", fg="black", bg="white")
    create = tkinter.Button(window, text="Create an account", fg="white", bg="black")
    sign_in = tkinter.Button(window, text="Login", fg="white", bg="black", command=Login)
    quit = tkinter.Button(window, text="Quit", fg="white", bg="black", command=window.destroy)
    window.configure(background="#008bb5")
    photo = tkinter.PhotoImage(file="Eating.gif")
    w = tkinter.Label(window, image=photo)
    w.pack()
    question.pack()
    create.pack()
    sign_in.pack()
    quit.pack()
    window.mainloop()

Mainscreen()