除了记录在案的类之外,ttk.Spinbox类和tk.Spinbox类之间有什么区别?
我发现一些不同之处,例如:
在按下箭头后,
'%d',而tk.Spinbox打印'up'或'down' (工作示例如下)是bug吗?如果是,有没有办法解决这个问题?我想使用ttk.Spinbox,因为tk.Spinbox没有正确设置样式,没有.set(),等等。
下面的代码生成两个Spinboxes。第一个继承自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进行了更新,其中包括在ttk中添加了Spinbox。据我所知,在tkinter8.5中,没有特殊的ttk.Spinbox,尽管ttk.Spinbox确实遵循了ttk的风格等等,所以一定有定义的东西,对吧?
我在tkinter 8.6和8.5中都尝试了这一点(假设没有Spinbox,但仍然正确地设置了样式),并得到了相同的结果。也就是说,ttk.Spinbox打印 '%d'**,,而tk.Spinbox打印** 'up' 'down'.或然而,validatecommand在这两种情况下都能按预期工作。
我检查了the Tk source code bug repository的错误报告,但没有发现这个问题。我是否正确使用了ttk.Spinbox的命令?
发布于 2019-02-19 22:56:18
使用Tcl/Tk:
按箭头后,
-to值必须为set.
实际上是tk.spinbox中的一个bug --它做的太多了。如果输入是有效的,向上或向下操作都不会使其无效。在or上按下ttk.Spinbox箭头按钮时,
OSX小部件实现是独立于窗口的,两者都试图与本机外观相匹配。检索方向的实现issue.
这里的问题是,python在tcl_do_upon_clicking_arrows中做了什么?在Tk中没有特定的绑定可以做到这一点。Tk中没有返回up/down的%d绑定参数。
这是一个python tkinter问题。
在Tk中,将使用对关联变量或验证命令的跟踪来跟踪更改。
发布于 2019-02-21 04:08:44
在继承上述问题的类之前,我通过修改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()函数可以替换任何具有所需行为的内容。
https://stackoverflow.com/questions/54753119
复制相似问题