Python Forum
[Tkinter] Validating and modifying a Spinbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Validating and modifying a Spinbox
#2
After going through the documentation Tcl8.6.13/Tk8.6.13 Documentation > Tk Commands > spinbox I found out that:

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
Reply


Messages In This Thread
Validating and modifying a Spinbox - by pfdjhfuys - May-20-2023, 03:24 PM
RE: Validating and modifying a Spinbox - by pfdjhfuys - May-21-2023, 06:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Add a SpinBox to MsgBox or Carry Variable Over? Extra 6 1,919 Jun-05-2022, 09:32 PM
Last Post: Extra
  [Tkinter] how to celect com port from spinbox and make connect button 00alkskodi00 0 2,504 Apr-20-2020, 02:26 PM
Last Post: 00alkskodi00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020