2

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()

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.

Dr Jeep
  • 43
  • 3
  • How are you calling that function? Which GUI framework are you using? Is it Tkinter? – PM 2Ring Mar 05 '16 at 10:42
  • Hey PM - I am using tkinter. i have wrapped the call to the function as; `code`vcmd=(root.register(okay),'%s','%P') – Dr Jeep Mar 05 '16 at 13:08
  • updated original question - i think it is a loss of focus thing possibly {:-{ but no idea why?? – Dr Jeep Mar 07 '16 at 00:12
  • You probably need to give us a [mcve] so we can try to reproduce this problem. – PM 2Ring Mar 07 '16 at 07:18
  • Hi PM - the code above is the full code now, sorry i should have posted it in its entirety in the first place. Thanks for your time in advance if you can point me in the right direction with this problem. – Dr Jeep Mar 07 '16 at 07:54
  • Sorry, I can't help because I'm running Python 2, and my Tkinter is a bit different to yours. – PM 2Ring Mar 07 '16 at 10:11
  • OK - I appreciate your time looking at it for me though :-) – Dr Jeep Mar 07 '16 at 11:30

1 Answers1

0

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

Dr Jeep
  • 43
  • 3