【问题标题】:Where is the text parameter in Tkinter Button class?Tkinter Button 类中的 text 参数在哪里?
【发布时间】:2018-05-18 17:31:44
【问题描述】:

这里是 Python 初学者!

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

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

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

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

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

【问题讨论】:

    标签: python button tkinter


    【解决方案1】:

    Button 类将其所有关键字参数 (**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)
    

    【讨论】:

    • 我可能需要研究 _init_ 的作用才能理解这一点。
    • __init__ 在初始化(创建)对象时调用。 python 文档有一篇很好的文章here。此外,您可能还想阅读继承 here
    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2016-08-29
    相关资源
    最近更新 更多