下面是tkinter.ttk從/usr/lib/python3.8/tkinter/ttk.py. 我注意到在這行代碼中self.tk.call(self._w, "set", value),self._w使用了而不是self。這發生在整個源代碼中ttk.py。我可以知道為什么要這樣做嗎?
class Spinbox(Entry):
"""Ttk Spinbox is an Entry with increment and decrement arrows
It is commonly used for number entry or to select from a list of
string values.
"""
def __init__(self, master=None, **kw):
"""Construct a Ttk Spinbox widget with the parent master.
STANDARD OPTIONS
class, cursor, style, takefocus, validate,
validatecommand, xscrollcommand, invalidcommand
WIDGET-SPECIFIC OPTIONS
to, from_, increment, values, wrap, format, command
"""
Entry.__init__(self, master, "ttk::spinbox", **kw)
def set(self, value):
"""Sets the value of the Spinbox to value."""
self.tk.call(self._w, "set", value)
uj5u.com熱心網友回復:
每個小部件代表嵌入式 tcl 解釋器中的一個物件。這些物件具有唯一的名稱(例如:.frame.another_frame.some_button)。這個名字是一個字串。
這個小部件名稱也是 tcl 解釋器中的一個命令。因此,為了呼叫底層小部件命令,您必須使用字串名稱。self._w包含字串。直接使用self._w而不是轉換self為字串是最有效的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/361387.html
