3

I wonder what is the meaning of .xxxxxx (e.g. .50109912) in Python Tkinter. I was trying to check what is returned by Widget_name(container, **configuration options).pack() Of course it will return None But when I check what is returned by the widget before packing, it gives something as this .50109912. This is how I got it in IDLE Python3.3.

>>> from tkinter import *
>>> root = Tk()
>>> mybutton = Button(root, text="Click Me", command=root.destroy)
>>> print(mybutton)
.50109912
1
  • That's the string representation of your button. Widget classes override their __str__ methods to return a decimal-looking string instead of the usual <Tkinter.Button instance at ...> that you usually see when you print an object. But I'm guessing what you're really asking is "what do these numbers represent?". I have no idea.
    – Kevin
    Commented Jan 8, 2015 at 18:15

2 Answers 2

7

The number 50109912 is the unique Python object id of the button widget:

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

Moreover, the string .50109912 is the button widget's window path name. Window path names are used internally by the TCL interpreter to keep track of widgets as well as what their parents are. In other words, they are paths for the interpreter to follow in order to reach particular widgets.

You'll notice too that 50109912 is the same number returned by the winfo_name method:

>>> mybutton.winfo_name()
'38321104'
>>>

Note however that winfo_name only returns the last portion of a widget's window path name (its object id). To get the full path, you need to call widget.__str__() by doing either str(widget) or print(widget).


The documentation for calling widget.__str__() can found through help:

>>> import tkinter
>>> help(tkinter.Button.__str__)
Help on function __str__ in module tkinter:

__str__(self)
    Return the window path name of this widget.

>>>

In addition, you may be interested in the Basic Widget Methods page over on Effbot (specifically, the section which talks about the .winfo_* methods). It contains information about how to get specific parts of a widget's window path name.


Also, if you want the Python representation of the object, you can use repr:

>>> from tkinter import *
>>> root = Tk()
>>> mybutton = Button(root, text="Click Me", command=root.destroy)
>>> print(repr(mybutton))
<tkinter.Button object at 0x0248BBD0>
>>>
1
  • Thank you iCodeZ for this comprehensive answer.
    – Mohammed
    Commented Jan 8, 2015 at 18:45
3
import tkinter as tk
root = tk.Tk()
button = tk.Button(root)
frame = tk.Frame(root)
subframe = tk.Frame(frame)
label = tk.Label(subframe)

for widget in (root, button, frame, subframe, label):
    print('{:<8} id {:<20} str {!s:30} '.format(type(widget).__name__, id(widget), widget))

yields

Tk       id 140490446651632      str .                              
Button   id 140490446651744      str .140490446651744               
Frame    id 140490446651688      str .140490446651688               
Frame    id 140490417530808      str .140490446651688.140490417530808 
Label    id 140490417531368      str .140490446651688.140490417530808.140490417531368 

As you can see, the str of the widget is a . for the root widget, and is a dot-separated sequence of id numbers for child widgets. The sequence of id numbers shows the lineage of the widget.

1
  • Thank you, unutbu, for this consise, yet professional answer. By virtue of your answer, I also came to konw the uses of {:<x} notation. I will use it quite often from now onward. One more remark about you is that 253k reputation is quite impressive. Wow!!
    – Mohammed
    Commented Jan 8, 2015 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.