他们之间的行为差异是什么ttk.数字调整框以及tk.数字调整框(ttk中的箭头方向车是不是有问题?)

2024-04-16 14:10:17 发布

您现在位置:Python中文网/ 问答频道 /正文

它们之间有什么区别ttk.数字调整框类和tk.数字调整框类,除了那些记录在案的类?在

我发现了一些差异,例如:

  1. 在ttk.数字调整框按下箭头后设置为0,同时tk.数字调整框没有。在
  2. 在ttk.数字调整框不通过按箭头验证更改,而tk.数字调整框做。在
  3. 在ttk.数字调整框按下OSX时箭头按钮不亮tk.数字调整框箭头按钮可以。在窗户上,两个灯都亮着。在
  4. 最关键的是,检索方向是为了tk.数字调整框但不是为了ttk.数字调整框: ttk.数字调整框打印'%d',而tk.数字调整框打印'up'或{}(下面的工作示例)

后者是一个bug吗?如果是,有没有办法解决这个问题?我想用ttk.数字调整框作为tk.数字调整框没有正确设置样式,没有.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有了一个更新,它为ttk添加了一个Spinbox。据我所知,在tkinter 8.5中,没有特殊的ttk.数字调整框不过ttk.数字调整框是遵循ttk的风格等等,所以一定有定义,对吧?在

我在tkinter8.6和8.5中都尝试了这种方法(假设没有Spinbox,但仍然正确地设置了样式),并且得到了相同的结果。即,ttk.数字调整框打印'%d',而tk.数字调整框打印'up''down'。然而,validatecommand在这两种情况下都能正常工作。在

我检查了the Tk source code bug repository的错误报告,但没有发现这个问题。我在用吗ttk.数字调整框的命令正确吗?在


Tags: selfinittkinterdef数字箭头validatetcl
2条回答

使用Tcl/Tk:

  • 在ttk.数字调整框按下箭头后设置为0,同时tk.数字调整框 没有。在

    必须设置-to值。

  • 在ttk.数字调整框不通过按箭头验证更改,而tk.数字调整框做。在

    实际上是个窃听器tk.旋转框它做得太多了。 如果条目有效,则向上或向下操作不能使其无效。

  • 在ttk.数字调整框按下OSX时箭头按钮不亮tk.数字调整框 箭头按钮可以。在窗户上,两个灯都亮着。在

    OSX小部件实现与windows是分开的,两者都试图 与本土风格相匹配。执行问题。

  • 最关键的是,找回方向tk.数字调整框但不是为了ttk.数字调整框: ttk.数字调整框打印“%d”,而tk.数字调整框打印“向上”或“向下”(下面的工作示例)

    这里的问题是,当单击箭头时,python在tcl内做什么? 在Tk中没有特定的绑定可以做到这一点。没有%d绑定参数 在Tk中返回上/下。在

    这是一个python-tkinter问题。在

    在Tk语言中,对相关变量或验证命令的跟踪 将用于跟踪更改。

我通过修改ttk.数字调整框在继承上述问题中的类之前,要使用生成的<<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)

然后呢

^{pr2}$

按下这些按钮后,它会将“向上”或“向下”打印到控制台。当然,do_upon_clicking_arrows()函数可以代替任何具有所需行为的东西。在

相关问题 更多 >