1

What are the differences between ttk.Spinbox classes and tk.Spinbox classes, except for those that are documented?

I found a few differences e.g.:

  1. ttk.Spinbox sets to 0 after pressing the arrows, while tk.Spinbox does not.
  2. ttk.Spinbox does not validate changes by pressing arrows, while tk.Spinbox does.
  3. The ttk.Spinbox arrow buttons don't light up when pressed on OSX while tk.Spinbox arrow buttons do. On Windows, both light up.
  4. Crucially, retrieving the direction works for tk.Spinbox, but not for ttk.Spinbox: ttk.Spinbox prints '%d', while tk.Spinbox prints 'up' or 'down' (working example below)

Is the latter a bug? If so, is there a way to fix this? I would like to use ttk.Spinbox as tk.Spinbox does not set Style correctly, does not have .set(), etc.

The following code generates two Spinboxes. The first inherits from tkinter and the second from ttk. Their modifications are identical.

import tkinter as tk
import tkinter.ttk as ttk


class TkSpinbox(tk.Spinbox):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, increment = 1, **kwargs)

        self.tcl_do_upon_clicking_arrows = self.register(self.do_upon_clicking_arrows)
        self.config(command = (self.tcl_do_upon_clicking_arrows, '%d'))

        self.tcl_validate = self.register(self.validate)
        self.config(validate = 'all', validatecommand = (self.tcl_validate, '%d'))

    def do_upon_clicking_arrows(self, direction):
        print(direction)

    def validate(self, typeOfAction):
        print(typeOfAction)
        return(True)


class TtkSpinbox(ttk.Spinbox):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, increment = 1, **kwargs)

        self.tcl_do_upon_clicking_arrows = self.register(self.do_upon_clicking_arrows)
        self.config(command = (self.tcl_do_upon_clicking_arrows, '%d'))

        self.tcl_validate = self.register(self.validate)
        self.config(validate = 'all', validatecommand = (self.tcl_validate, '%d'))

    def do_upon_clicking_arrows(self, direction):
        print(direction)

    def validate(self, typeOfAction):
        print(typeOfAction)
        return(True)


root = tk.Tk()

tkSpinbox = TkSpinbox()
ttkSpinbox = TtkSpinbox()
tkSpinbox.grid()
ttkSpinbox.grid()

root.mainloop()

In december 2018 there was an update to tkinter 8.6 which adds among other things a Spinbox to ttk. As I understand it, in tkinter 8.5, there is no special ttk.Spinbox, though ttk.Spinbox does follow ttk's Style etc. So there must be something defined, right?

I tried this in both tkinter 8.6 and 8.5 (that supposedly does not have a Spinbox yet still sets the Style correctly) and had the same result. Namely, ttk.Spinbox prints '%d', while tk.Spinbox prints 'up' or 'down'. Yet, validatecommand does work as expected in both cases.

I checked the Tk source code bug repository for bug reports, but didn't find this problem. Am I using ttk.Spinbox's command correctly?

2 Answers 2

1

With Tcl/Tk:

  • ttk.Spinbox sets to 0 after pressing the arrows, while tk.Spinbox does not.

    The -to value must be set.

  • ttk.Spinbox does not validate changes by pressing arrows, while tk.Spinbox does.

    Actually a bug in tk.spinbox -- it is doing too much. If the entry is valid, an up or down action cannot make it invalid.

  • The ttk.Spinbox arrow buttons don't light up when pressed on OSX while tk.Spinbox arrow buttons do. On Windows, both light up.

    The OSX widget implementation is separate from windows and both attempt to match the native look and feel. An implementation issue.

  • Crucially, retrieving the direction works for tk.Spinbox, but not for ttk.Spinbox: ttk.Spinbox prints '%d', while tk.Spinbox prints 'up' or 'down' (working example below)

    The question here is, what is python doing inside of tcl_do_upon_clicking_arrows? There is no particular binding in Tk that does this. There is no %d bind parameter in Tk that returns up/down.

    This is a python tkinter issue.

    In Tk, a trace on the associated variable or the validation command would be used to track changes.

4
  • On your last point: there does seem to be a %d bind parameter in Tk as documented here
    – Stendert
    Feb 19, 2019 at 15:20
  • The %d bind parameter is a 'detail' field, not up/down. It is not used for button presses and button releases.
    – Brad Lanam
    Feb 19, 2019 at 15:36
  • Could you explain what bind parameters and detail fields are and how they relate? Thanks
    – Stendert
    Feb 20, 2019 at 19:53
  • The documentation for bind explains everything.
    – Brad Lanam
    Feb 21, 2019 at 0:51
0

I solved the crucial misbehaviour (fourth point) by modifying the ttk.Spinbox to make use of the generated <<Increment>> and <<Decrement>> events, before inheriting from the class in the question above:

class UpDownSpinbox(ttk.Spinbox):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # bind the events that are triggered by pressing an arrow button  
        self.bind("<<Increment>>", self._do_on_increment)
        self.bind("<<Decrement>>", self._do_on_decrement)

    def _do_on_increment(self, *args, **kwargs):
        self.do_upon_clicking_arrows("up")
        # optional, prevent the event handler from doing other things by returning "break"
        return("break")

    def _do_on_decrement(self, *args, **kwargs):
        self.do_upon_clicking_arrows("down")
        # optional, prevent the event handler from doing other things by returning "break"
        return("break")

    def do_upon_clicking_arrows(self, direction):
        print(direction)

and then

class TtkSpinBox(UpDownSpinbox):
    def __init__(    # ..... fill in as in original question.

this prints "up" or "down" to the console upon pressing those buttons. The do_upon_clicking_arrows() function can of course be substituted for anything with desired behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.