Search code examples
pythonttk

How do I get the value off the spinbox in ttk?


I now have a working spinbox, but I can't find out how to get the value of the spinbox. When looking on the internet I couldn't get the examples working, can someone help?

This is what I now have.

spinbox = StringVar()
s = ttk.Spinbox(root, from_=1.0, to=100.0, textvariable=spinbox).grid(column=4, row=1, sticky=W)
ttk.Label(root, text="amount:").grid(column=3, row=1, sticky=W)

Just having the number get printed in the console is good enough.

Thanks,


Solution

  • The problem is that SpinBox.grid() doesn't return anything i.e. returns None.

    So do it this way:

    s = ttk.Spinbox(root, from_=1.0, to=100.0, textvariable=spinbox)
    s.grid(column=4, row=1, sticky=W)
    print(s.get())
    

    Edit- Add a function to get the value and add that function to command parameter

    def get_spinbox_val():
        global s
        print(s.get())
    
    s = ttk.Spinbox(root, from_=1.0, to=100.0, textvariable=spinbox, command=get_spinbox_val)
    s.grid(column=4, row=1, sticky=W)