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.:
- ttk.Spinbox sets to 0 after pressing the arrows, while tk.Spinbox does not.
- ttk.Spinbox does not validate changes by pressing arrows, while tk.Spinbox does.
- The ttk.Spinbox arrow buttons don't light up when pressed on OSX while tk.Spinbox arrow buttons do. On Windows, both light up.
- 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?