消息框停止验证 [英] messagebox stops validation

查看:57
本文介绍了消息框停止验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么消息框(或简单对话框)会破坏以下代码的流程.该代码基本上验证了 python 3.5 中的输入框.它检查该字段是否仅包含数字值并且长度不超过 9 位,但输入框可以为空.向用户添加一条消息,在他们确定后,允许输入框超过 9 位并接受字母,这当然是我不希望的.

I don't understand why a messagebox (or simpledialog) breaks the flow of the following code. The code basically validates an entry box in python 3.5. It checks that the field only contain numeric values and that it does't go over 9 digits long, the entry box can be empty though. The addition of a message to the user, after they OK it, allows the entry box to be more than 9 digits and accepts letter which of course I don't want it to do.

from tkinter import *
from tkinter import simpledialog
from tkinter import messagebox
root = Tk()


root.title("Zebra")
root.update_idletasks()
root.geometry("350x200+600+300")
root.config(bg="blue")

def okay(old,new): #,c,d,e,f,g,h):

    try:
        x = int(new)
    except ValueError as ex:
        if len(new) == 0:
            return True
        else:
            return False
    else:

        if len(new) > 9:
            messagebox.showerror("Error","Entry is too long")
           # When messagebox is removed or commented out all is working ok
           # but add below line and BINGO it works again :-)

            txtNorm.config(validate='all', vcmd=vcmd) 
           # New line above as of 08/03/2016 brings validation back.
            return False

        elif len(new) <=9:
            return True
    finally:

        if len(new) > 9:

            return False


        pass
def txtNormToggle(event): # When the user double clicks the field to enter or correct a number.
    txtNorm.config(state="normal")
def txtNormFinished(a):
    txtNorm.config(state="disabled")
    root.focus()

vcmd=(root.register(okay),'%s','%P')


txtNorm = Entry(root)
txtNorm.grid(row=1, column=1,padx=(15,15),pady=(15,15), sticky=E+W)
txtNorm.insert(0,"123")
txtNorm.config(state="disabled", justify="center", validate='all', vcmd=vcmd)
txtNorm.bind('<Button>',txtNormToggle)
txtNorm.bind('<Control-z>',txtNormFinished)
txtNorm.bind('<Escape>',txtNormFinished)
txtNorm.bind('<Return>',txtNormFinished)


root.mainloop()

上面没有消息框会阻止用户输入除数字以外的任何内容,这是我想要的,一旦单击确定,消息框输入字段允许超过 9 位数字和其他字符

The above without the messagebox stops the user entering anything other than digits, which i want, with messagebox once OK is clicked the entry field allows more than 9 digits and other characters

好的,所以我创建了自己的弹出子窗口,并且验证仍然超出窗口,怀疑这与主窗口失去焦点有关,杀死了输入框的验证.请有任何想法.

edit: ok so I have created my own pop-up child window and the validation still goes out the window, suspect it is something to do with loss of focus from the main window killing the validation from the entry box. Any ideas please.

推荐答案

我已经在(好的)函数的输入框中添加了验证,但它没有向我解释为什么会发生验证丢失.代码现在按我想要的方式工作

i have added validate back to the entry box in the (okay) function, it does't explain to me why the loss of validation occurs though. The code works now how i wanted

这篇关于消息框停止验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆