How to get current spinbox 'from' and 'to' values

393 views Asked by At

How to get the values of spinbox from and to in a function?

sbDays=tk.Spinbox(frame,from_=0,to=366)
sbDays.place(relx=initialX,rely=yDistance)
sbDays.configure(validate='all',validatecommand=(windows.register(validate),'%P'))

def validate(userInput): 
    if userInput=="":
        return True

    try:
        val=int(float(userInput))
    except ValueError:
        return False
    return val>=0 and val<=366

Instead of return val>=0 and val<=366. I need this:

minVal=spinbox 'from' value of '0'
maxVal=spinbox 'to' value of '366'

return val>=minVal and val<=maxVal

In C#, something like this:

minVal=this.From()
maxVal=this.To()
2

There are 2 answers

7
Henry On

You can use the cget method to get attributes from a widget.
In this case, you need minVal = sbDays.cget("from") and maxVal = sbDays.cget("to")

Edit - For multiple spinboxes
To use this with multiple spinboxes, change the validatecommand to
validatecommand=(windows.register(validate),'%P', '%W')
and change validate(userInput) to validate(userInput, widget). Then replace sbDays in my answer with windows.nametowidget(widget) and it should work.
The %W in the validatecommand gives the name of the widget (from here), which is then used to get the widget with nametowidget (from here).

2
TheLizzard On

You can also use the .config method like this: sbDays.config("from") and sbDays.config("to"). It returns something like ('from', 'from', 'From', 0, 0.0) or ('to', 'to', 'To', 0, 366.0). Note the last value is the one we need so using sbDays.config(...)[-1] should give the desired result.