May-21-2023, 06:57 AM
After going through the documentation Tcl8.6.13/Tk8.6.13 Documentation > Tk Commands > spinbox I found out that:
I was overthinking the problem.
The code below worked:
Quote:In general, the -textvariable and -validatecommand can be dangerous to mix. Any problems have been overcome so that using the -validatecommand will not interfere with the traditional behavior of the spinbox widget. Using the -textvariable for read-only purposes will never cause problems. The danger comes when you try set the -textvariable to something that the -validatecommand would not accept, which causes -validate to become none (the -invalidcommand will not be triggered). The same happens when an error occurs evaluating the -validatecommand.
Primarily, an error will occur when the -validatecommand or -invalidcommand encounters an error in its script while evaluating or -validatecommand does not return a valid Tcl boolean value. The -validate option will also set itself to none when you edit the spinbox 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 value of the widget during validation and still have the -validate option set, you should include the command
I was overthinking the problem.
The code below worked:
def ValidateIfNum(self, user_input, widget_name): """ Args: user_input (IntVal): The value typed into the spinbox widget_name (): The widget name Returns: Boolean: Whether the value is valid """ valid = user_input.isdigit() # now that we've ensured the input is only integers, range checking! if valid: # get minimum and maximum values of the widget to be validated minval = int(self.root.nametowidget(widget_name).config('from')[4]) maxval = int(self.root.nametowidget(widget_name).config('to')[4]) # check if it's in range if int(user_input) not in range (minval, maxval): valid = False return valid