Tkinter 中的小部件 类 还是方法?

Are Widgets Classes or Methods in Tkinter?

文本小部件是使用Text() 方法创建的。

import tkinter as tk 
root = tk.Tk()
T = tk.Text(root, height=2, width=30) 
T.pack() 
T.insert(tk.END, "Just a text Widget\nin two lines\n") 
w = tk.Label(root, text="Hello Tkinter!") 
w.pack() 
root.mainloop()

我是 Python 的新手。我的理解是 TextLabel 是 类 而 Tw 是从 TextLabel 创建的对象 类。但是在上面的文本示例中,一个网站提到

A text widget is created by using the Text() method.

我现在完全糊涂了。 pack() 是一种方法,我们可以将方法应用于我们从 类 创建的对象(此处为 Tw),例如 LabelText.

请告诉我 LabelTextButton 等小部件是 类 还是方法。

Tkinter 小部件是 classes。

But in the above text example, a website mentioned that A text widget is created by using the Text() method.

该网站不正确。它们是 classes,您可以通过查看 tkinter 的源代码来验证这一点,您会在其中看到每个小部件的 class 定义(TextLabel, Frame, 等等).

例如,文本小部件的第一部分如下所示(取自 tkinter 的 __init__.py 文件):

class Text(Widget, XView, YView):
    """Text widget which can display text in various forms."""
    def __init__(self, master=None, cnf={}, **kw):
        """Construct a text widget with the parent MASTER.

        STANDARD OPTIONS

            background, borderwidth, cursor,
            exportselection, font, foreground,
            highlightbackground, highlightcolor,
            highlightthickness, insertbackground,
            insertborderwidth, insertofftime,
            insertontime, insertwidth, padx, pady,
            relief, selectbackground,
            selectborderwidth, selectforeground,
            setgrid, takefocus,
            xscrollcommand, yscrollcommand,

        WIDGET-SPECIFIC OPTIONS

            autoseparators, height, maxundo,
            spacing1, spacing2, spacing3,
            state, tabs, undo, width, wrap,

        """
        Widget.__init__(self, master, 'text', cnf, kw)

inspect 模块可能会帮助您消除困惑。

In [37]: import inspect

In [38]: from tkinter import Text

In [39]: T = Text()

In [40]: inspect.isclass(Text)
Out[40]: True

In [41]: inspect.ismethod(Text)
Out[41]: False

In [42]: inspect.ismethod(T.pack)
Out[42]: True