亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Tkinter 中的 Widgets 是類還是方法?

Tkinter 中的 Widgets 是類還是方法?

MM們 2022-12-06 15:23:50
使用該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 的新手。我的理解是,Text和Label是類,T并且w是從Text和Label類創建的對象。但是在上面的文本示例中,一個網站提到使用該Text()方法創建文本小部件。我現在完全糊涂了。pack()是一種方法,我們可以在我們從類創建的對象(T和這里)上應用方法和。wLabelText請讓我知道 、 、 等小部件Label是Text類Button還是方法。
查看完整描述

2 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

Tkinter 小部件是類。

但是在上面的文本例子中,某網站提到A text widget is created by using the Text() method.

那個網站不正確。它們是類,您可以通過查看 tkinter 的源代碼來驗證這一點,您會在其中看到每個小部件(Text、Label、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)


查看完整回答
反對 回復 2022-12-06
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

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


查看完整回答
反對 回復 2022-12-06
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號