1

Consider this simple code:

from tkinter import *
root = Tk()

def update_label():
    my_label.config(text = my_var.get())


my_label = Label(root, text = "?")
my_label.pack()
my_var = IntVar()
Spinbox(root, from_ = 1, to = 10,  textvariable = my_var, command = update_label).pack()
root.mainloop()

The documentation at New Mexico Tech states that:

The user can also enter values directly, treating the widget as if it were an Entry.

However if I enter a value directly in the SpinBox from the above code, it does not trigger the command callback.

Further, I can enter any value in the SpinBox widget despite the fact that I have restricted its -from and _to values between 1 and 10.

My questions:

  1. Can I do something so that a direct entry in spinbox triggers the command callback ?

  2. Can I restrict the value that can be entered directly within 1 and 10 ?

bhaskarc
  • 9,269
  • 10
  • 65
  • 86

1 Answers1

1

You can add a trace to my_var which can be called everytime the value changes. For a little more information, see:

What are the arguments to Tkinter variable trace method callbacks?

You can do data validation by using the validate and validatecommand attributes of the spinbox. It works exactly the same as with the Entry widget, which is covered here:

Interactively validating Entry widget content in tkinter

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685