1

我在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()
4

2 回答 2

1

您需要将日志窗口设置为对话框的父级:

messagebox.showerror("error", "try again", parent=log)

默认情况下,它将使用根窗口(Tk实例)作为父窗口,在这种情况下这不是您想要的。

于 2017-12-05T11:38:44.070 回答
0

在@furas 的提示下,如何实现这一点:

当条目不匹配时创建另一个调用它的函数,并对顶层窗口使用grab_set方法tp.grab_set()。您可以将自定义图像添加到顶层窗口以及要在框中显示的消息(这里:我使用标签来描绘那)

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()
于 2017-12-05T12:58:44.997 回答