image for that
我这里有几行代码是登录系统工作正常,但是当我提供错误的密码而不关闭Toplevel
时,我可以多次点击messagebox
按钮。我怎样才能这样做在我再次尝试之前必须关闭messagebox
。
from tkinter import *
from tkinter import messagebox
def top():
if entry1.get() == "333":
log.destroy()
root.deiconify()
else:
messagebox.showerror("error", "try again")
root = Tk()
root.geometry("300x300")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()
答案 0 :(得分:1)
您需要将日志窗口设置为对话框的父级:
messagebox.showerror("error", "try again", parent=log)
默认情况下,它将使用根窗口(Tk
实例)作为父窗口,在这种情况下,它不是您想要的。
答案 1 :(得分:0)
通过@furas的提示,如何实现这个:
在条目不匹配时为其调用创建另一个函数,并在Toplevel窗口tp.grab_set()
中使用grab_set方法。您可以将您的惯常图像添加到Toplevel窗口以及在框中显示的消息(这里:我用标签来描绘)
from tkinter import *
from tkinter import messagebox
def dialog(): # this function to call when entry doesn't match
tp = Toplevel(log)
tp.geometry("300x100")
tp.title('error')
tp.grab_set() # to bring the focus to the window for you to close it
tp.resizable(width=False, height=False)
l = Label(tp, text="try again\n\n\n\n add your customarize image to the window")
l.pack()
def top():
if entry1.get() == "333":
log.destroy()
root.deiconify()
else:
dialog() # being called here
root = Tk()
root.geometry("300x300")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()