gpt4 book ai didi

python - 如何在 tkinter 中按下按钮时调用函数?

转载 作者:行者123 更新时间:2023-11-30 22:00:00 26 4
gpt4 key购买 nike

我已经编写了代码,以便当按下“登录”按钮时它会调用“登录”函数。但是,每次我运行代码并按下按钮时,都会显示错误消息“_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()

按下“登录”按钮时,应销毁主屏幕并打开“登录”屏幕。但是,目前每当在主屏幕上按下登录按钮时,主屏幕都会保持打开状态,并且登录屏幕将显示为空白屏幕。

最佳答案

这应该有效。注意

的使用
`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()

关于python - 如何在 tkinter 中按下按钮时调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54437368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com