Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of .xxxxxxx in Python Tkinter

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
like image 804
Mohammed Avatar asked Nov 20 '25 06:11

Mohammed


2 Answers

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>
>>>
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.

like image 27
unutbu Avatar answered Nov 22 '25 19:11

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!