Python ttk对象-不了解特定于窗口小部件的选项 - python

我昨天实现了一个ttk进度栏,看到了一些我不太理解的代码。

可以使用以下类似方法为进度栏设置最大值:

progress_bar["maximum"] = max

我期望ttk Progressbar对象将使用实例变量来跟踪已创建对象的最大值,但是该语法看起来更像:

progres_bar.maximum = max

所以我的问题是,括号内的语法究竟发生了什么,术语是什么,我在哪里可以读到更多?当我查看Progressbar类时,我看到的只是

class Progressbar(Widget):
    """Ttk Progressbar widget shows the status of a long-running
    operation. They can operate in two modes: determinate mode shows the
    amount completed relative to the total amount of work to be done, and
    indeterminate mode provides an animated display to let the user know
    that something is happening."""

    def __init__(self, master=None, **kw):
        """Construct a Ttk Progressbar with parent master.

        STANDARD OPTIONS

            class, cursor, style, takefocus

        WIDGET-SPECIFIC OPTIONS

            orient, length, mode, maximum, value, variable, phase
        """
        Widget.__init__(self, master, "ttk::progressbar", kw)

我看到有一个“ widget-specifc选项”,但我不明白progress_bar["maximum"] = max如何设置该值或如何存储它。

python大神给出的解决方案

发生的事情是,ttk模块是安装了tk软件包的tcl解释器的薄包装。 Tcl / tk没有python类的概念。

在tcl / tk中,设置属性的方法是使用函数调用。例如,要设置maximum属性,您可以执行以下操作:

.progress_bar configure -maximum 100

TTK包装器非常相似:

progress_bar.configure(maximum=100)

由于只有原始tkinter开发人员才知道的原因,他们决定实现一个字典接口,该接口允许您使用方括号表示法。也许他们认为这更像Python?例如:

progress_bar["maximum"] = 100

几乎可以肯定的是,他们之所以没有设置对象的这些属性(例如:progress_bar.maximum = 100),是因为某些tcl / tk小部件属性会与python保留字或标准属性(例如,id)冲突。通过使用字典,它们避免了这种冲突。