除了记录的那些之外,ttk.Spinbox类和tk.Spinbox类之间有什么区别?
我发现了一些差异,例如:
'%d'
,而tk.Spinbox打印'up'
或'down'
(下面的工作示例)后者是一个错误吗?如果是这样,有没有办法解决这个问题?我想使用ttk.Spinbox作为tk.Spinbox没有正确设置Style,没有.set()等。
以下代码生成两个Spinbox。第一个继承自tkinter,第二个继承自ttk。他们的修改是相同的。
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()
在2018年12月,对tkinter 8.6进行了更新,其中包括一个Spinbox to ttk。据我所知,在tkinter 8.5中,没有特殊的ttk.Spinbox,虽然ttk.Spinbox确实遵循ttk的Style等所以必须定义一些东西,对吧?
我在tkinter 8.6和8.5(据说没有Spinbox但仍然正确设置了Style)中尝试了这个并且得到了相同的结果。即,ttk.Spinbox打印'%d'
,而tk.Spinbox打印'up'
或'down'
。然而,validatecommand在两种情况下都能按预期工作。
我检查了the Tk source code bug repository的bug报告,但没有发现这个问题。我正确使用ttk.Spinbox的命令吗?
使用Tcl / Tk:
我通过修改ttk.Spinbox来解决关键的错误行为(第四点),以便在继承上述问题中的类之前使用生成的<<Increment>>
和<<Decrement>>
事件:
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)
然后
class TtkSpinBox(UpDownSpinbox):
def __init__( # ..... fill in as in original question.
按下这些按钮后,它会向控制台打印“向上”或“向下”。 do_upon_clicking_arrows()
函数当然可以替代任何具有所需行为的函数。