【发布时间】:2021-11-20 15:50:35
【问题描述】:
下面是从/usr/lib/python3.8/tkinter/ttk.py 中提取的tkinter.ttk 类。我注意到在代码行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)
【问题讨论】:
-
self._w存储小部件的名称,因此在tk.call()中使用。但是,您也可以使用self,只要将self用作字符串,因为Misc.__str__()(所有小部件都继承自它)返回self._w。