Steven Summers
Steven Summers

Reputation: 5384

Spinbox get() method issues

I'm making an application involving the SpinBox widget. My issue is that whenever I use the get() method to obtain the value after the widget has been clicked. I instead get the value before it was clicked. Here's an example of what I'm doing:

import tkinter as tk

class Example(object):
    def __init__(self, master):
        master.geometry('150x150')

        self._box = tk.Spinbox(master, from_ = 0, to = 100)
        self._box.pack(expand = True)
        self._box.bind('<ButtonRelease>', self.func)

    def func(self, event):
        print(int(self._box.get()))

root = tk.Tk()
app = Example(root)
root.mainloop()

What's being printed is

0 #1st click up | spinbox displays 1
1 #2nd click up | spinbox displays 2
2 #3rd click up | spinbox displays 3
# etc

Any help getting around this would be appreciated

Upvotes: 0

Views: 266

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

Rather than doing a bind, set the command attribute. This will always be called after the value has changed.

self._box = tk.Spinbox(..., command=self.func)
...
def func(self):
    ...

Upvotes: 1

Related Questions