我想知道 Python Tkinter 中 .xxxxxx
(例如 .50109912
)的含义是什么。我试图检查 Widget_name(container, **configuration options).pack()
返回的内容
当然它会返回 None
但是当我在打包前检查小部件返回的内容时,它会给出类似 .50109912
的内容。这就是我在 IDLE Python3.3 中得到它的方式。
>>> from tkinter import *
>>> root = Tk()
>>> mybutton = Button(root, text="Click Me", command=root.destroy)
>>> print(mybutton)
.50109912
最佳答案
数字 50109912
是按钮小部件的唯一 Python 对象 ID:
>>> from tkinter import *
>>> root = Tk()
>>> mybutton = Button(root, text="Click Me", command=root.destroy)
>>> print(mybutton)
.38321104
>>> id(mybutton)
38321104
>>>
此外,字符串.50109912
是按钮小部件的窗口路径名。窗口路径名称由 TCL 解释器在内部使用,以跟踪小部件及其父项。换句话说,它们是解释器为了到达特定的小部件而遵循的路径。
您还会注意到 50109912
与 winfo_name
返回的数字相同方法:
>>> mybutton.winfo_name()
'38321104'
>>>
但是请注意,winfo_name
仅返回小部件窗口路径名的最后一部分(其对象 ID)。要获得完整路径,您需要通过执行 str(widget)
或 print(widget)
来调用 widget.__str__()
。
调用widget.__str__()
的文档可以通过help
找到:
>>> import tkinter
>>> help(tkinter.Button.__str__)
Help on function __str__ in module tkinter:
__str__(self)
Return the window path name of this widget.
>>>
此外,您可能对 Basic Widget Methods 感兴趣在 Effbot 上翻页(特别是讨论 .winfo_*
方法的部分)。它包含有关如何获取小部件窗口路径名的特定部分的信息。
另外,如果你想要对象的 Python 表示,你可以使用 repr
:
>>> from tkinter import *
>>> root = Tk()
>>> mybutton = Button(root, text="Click Me", command=root.destroy)
>>> print(repr(mybutton))
<tkinter.Button object at 0x0248BBD0>
>>>
关于python - Python Tkinter 中.xxxxxxx 的含义是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27846699/