Python Tkinter中.xxxxxxx的含义是什么?

Moh*_*med 3 python tkinter python-3.x

我想知道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
Run Code Online (Sandbox Code Playgroud)

iCo*_*dez 7

该数字50109912是按钮小部件的唯一Python对象ID:

>>> from tkinter import *
>>> root = Tk()
>>> mybutton = Button(root, text="Click Me", command=root.destroy)
>>> print(mybutton)
.38321104
>>> id(mybutton)
38321104
>>>
Run Code Online (Sandbox Code Playgroud)

而且,字符串.50109912是按钮小部件的窗口路径名.窗口路径名称由TCL解释器在内部使用,以跟踪窗口小部件以及它们的父项.换句话说,它们是解释器遵循的路径,以便达到特定的小部件.

您也会注意到50109912该winfo_name方法返回的数字相同:

>>> mybutton.winfo_name()
'38321104'
>>>
Run Code Online (Sandbox Code Playgroud)

但请注意,winfo_name仅返回窗口小部件窗口路径名称的最后部分(其对象ID).要获得完整路径,您需要widget.__str__()通过执行任一操作str(widget)或调用print(widget).


调用文档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.

>>>
Run Code Online (Sandbox Code Playgroud)

此外,您可能对Effbot上的Basic Widget Methods页面感兴趣(具体来说,是讨论.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>
>>>
Run Code Online (Sandbox Code Playgroud)