title: tkinter : Spinbox, validatecommand and invalidcommand
body: <p>I'm trying to use a Spinbox widget in a script and I would like the Spinbox not to accept incorrect input. I would like the input to be an integer between 0 and 50. I found that I could use the option <code>validatecommand</code> to call a registered function to check the input, and the option <code>invalidcommand</code> to do stuff if the function called by <code>validatecommand</code> returns <code>False</code>. So what I want is: </p>
  
  <ul>
  <li>Check if input is correct</li>
  <li>if it's not, change the value in the Spinbox to be a default value (say 0)</li>
  </ul>
  
  <p>Here's my code for setting the Spinbox:</p>
  
  <pre><code>import tkinter as tk
  
  root = tk.Tk()
  
  validate_input = (root.register(correct_input), '%P')
  invalid_input = (root.register(incorrect_input), '%W')
  
  x_select = tk.Spinbox(root, from_ = 0, to = 50,
                        validate = 'all', validatecommand = validate_input,
                        invalidcommand = invalid_input)
  x_select.grid(row = 1, column = 0, columnspan = 2)
  </code></pre>
  
  <p>And here are the functions used to control the input and to change it back to a default value if necessary:</p>
  
  <pre><code>def correct_input(text):
      valid = False
      if text.isdigit():
          if (int(text) &lt;= 50 and int(text) &gt;= 0):
              valid = True
      elif text == '':
          valid = True
      return valid
  
  def incorrect_input(widget_name):
      widget = root.nametowidget(widget_name)
      widget.delete(0, 'end')
      widget.insert(0, 0)
  </code></pre>
  
  <p>Problem is, it only works once: if I type 'a' for instance the value in <code>x_select</code> will be set to <code>0</code> but then input is not checked anymore i.e. I can type whatever I want the input will not be corrected. What am I doing wrong here?</p>