Python tkInter Entry fun

前端 未结 3 508
猫巷女王i
猫巷女王i 2020-12-10 09:02

Playing around with Python - tkInter - Entry widget - when I use validatecommand (below), the check happens the first time the string > Max, but when I continue to enter tex

相关标签:
3条回答
  • 2020-12-10 09:29

    Here is a code sample that will limit the input to 5 characters:

    import Tkinter as tk
    
    master = tk.Tk()
    
    def callback():
        print e.get()
    
    def val(i):
        print "validating"
        print i
    
        if int(i) > 4:
            print "False"
            return False
        return True
    
    vcmd = (master.register(val), '%i')
    
    e = tk.Entry(master, validate="key", validatecommand=vcmd)
    e.pack()
    
    b = tk.Button(master, text="OK", command=lambda: callback())
    b.pack()
    
    tk.mainloop()
    

    I threw in a bunch of print statements so you can sort of see what it's doing in the console.

    Here are the other substitutions you can pass:

       %d   Type of action: 1 for insert, 0  for  delete,  or  -1  for  focus,
            forced or textvariable validation.
    
       %i   Index of char string to be inserted/deleted, if any, otherwise -1.
    
       %P   The value of the entry if the edit is allowed.  If you are config-
            uring  the  entry  widget to have a new textvariable, this will be
            the value of that textvariable.
    
       %s   The current value of entry prior to editing.
    
       %S   The text string being inserted/deleted, if any, {} otherwise.
    
       %v   The type of validation currently set.
    
       %V   The type of validation that triggered the callback (key,  focusin,
            focusout, forced).
    
       %W   The name of the entry widget.
    
    0 讨论(0)
  • 2020-12-10 09:33

    I'm sure exactly what the reason is, but I have a hunch. The validation check is done every time the entry is edited. I did some testing and found that it does indeed execute, and can do all sorts of things during the validation every time. What causes it to stop working correctly is when you edit it from within the validatecommand function. This causes it to stop calling the validate function any further. I guess it no longer recognizes further edits to the entry value or something.

    lgal Serban seems to have the behind the scenes info on why this occurs.

    0 讨论(0)
  • 2020-12-10 09:36

    From the Tk man:

    The validate option will also set itself to none when you edit the entry widget from within either the validateCommand or the invalidCommand. Such editions will override the one that was being validated. If you wish to edit the entry widget (for example set it to {}) during validation and still have the validate option set, you should include the command

    after idle {%W config -validate %v}

    don't know how to translate that to python.

    0 讨论(0)
提交回复
热议问题