Demiid
Demiid

Reputation: 13

How to call a function when a button is pressed in tkinter?

I have written my code so that when the button "SignIn" is pressed it calls the function "Login". However, every time I run the code and press the button, the error message "_tkinter.TclError: image "pyimage2" doesn't exist" is displayed and I cannot seem to find a solution which fixes my code.

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

When the SignIn button is pressed, the MainScreen should be destroyed and the Login screen should be opened. However, currently whenever the login button is pressed on the main screen, the MainScreen remains open and the Login screen is displayed as a blank screen.

Upvotes: 1

Views: 181

Answers (2)

Partho63
Partho63

Reputation: 3116

Okay so the problem is you are trying to run two instances of Tk() simultaneously which you should not. Reasons are described here and here also

Instead of window = tkinter.Tk() in your Login() you can use window = tkinter.Toplevel() to solve the problem like following:

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

Upvotes: 1

MEdwin
MEdwin

Reputation: 2960

This should work. Notice the use of

`tkinter.Toplevel()

and Image.open. This is because the button that calls the function is itself sitting in an active window.

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

Upvotes: 2

Related Questions