pythontkinteroverwriteboundsspinbox

How does one retroactively change the bounds of a tkinter Spinbox instance?


For my program I need to change the list of acceptable values based on updated information from the user. Is there any method to do that, or a property I can overwrite?

I tried simply:

col_spinbox.values = spin_vals

where spin_vals is the tuple of values I want, and col_spinbox is the instance for which I'm trying to overwrite the accepted values. But this didn't work.

From investigation it seems that the property values is stored as a string with spaces in between each value. So I formatted spin_vals that way, and it still didn't work. Is there a method that is intended to be used or is there just no way of overwriting the acceptable values?


Solution

  • One common way to change the properties or options of a tkinter widget is to use the universal config() widget method — and doing this to a Spinbox's values option does indeed work.

    Simple example.

    import tkinter as tk
    
    
    root = tk.Tk()
    
    spinbox = tk.Spinbox(root, values=('red', 'blue', 'green'))
    spinbox.pack()
    
    def new_values(spinbox):
        spinbox.config(values=('yellow', 'cyan', 'magenta'))
    
    chg_btn = tk.Button(root, text="Chg", command=lambda: new_values(spinbox))
    chg_btn.pack()
    
    root.mainloop()