1

Python初学者在这里!

我正在学习 Tkinter,当我遇到创建按钮时,他们使用了这个:

quitButton = Button(self, text="Quit")

我去检查了 __init__ 脚本中 tkinter 模块下的 Button 声明,它显示:

class Button(Widget):
"""Button widget."""
......
......

所以我的问题是,为什么我们能够传递一个文本参数?

4

1 回答 1

3

该类Button将其所有关键字 agruments ( **kw) 转发到其基类 ( Widget)。你可以在这里看到它是如何做到的

class Button(Widget):
    """Button widget."""
    def __init__(self, master=None, cnf={}, **kw):
        """Construct a button widget with the parent MASTER.

        STANDARD OPTIONS

            activebackground, activeforeground, anchor,
            background, bitmap, borderwidth, cursor,
            disabledforeground, font, foreground
            highlightbackground, highlightcolor,
            highlightthickness, image, justify,
            padx, pady, relief, repeatdelay,
            repeatinterval, takefocus, text,
            textvariable, underline, wraplength

        WIDGET-SPECIFIC OPTIONS

            command, compound, default, height,
            overrelief, state, width
        """
        Widget.__init__(self, master, 'button', cnf, kw)
于 2018-05-18T17:33:49.160 回答