What is the point of .xxxxxxx in Python Tkinter

I wonder what is the point .xxxxxx

(for example .50109912

) in Python Tkinter. I tried to check what is returned Widget_name(container, **configuration options).pack()

Of course it will return None

But when I check what is returned to the widget before packing it gives something like 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

      

+3


source to share


2 answers


Number 50109912

is a unique Python object identifier for the button widget:

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

      

Also, string .50109912

is the path name of the button widget window. The default window names are used by the TCL interpreter to keep track of widgets as well as their parents. In other words, this is the path for the interpreter to follow in order to reach certain widgets.

You will also notice that 50109912

- this is the same number returned by the method winfo_name

:

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

      

Note, however, that winfo_name

only returns the last part of the widget 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 the call widget.__str__()

can be found via help

:

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

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

>>>

      

Also, you may be interested in the Basic Widget Types page on the Effbot page (in particular, the section that talks about .winfo_*

methods). It contains information on how to get specific parts of the widget window path name.




Also, if you want a representation of a Python 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>
>>>

      

+7


source


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

      

gives



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

widget represents the .

root widget and is a dotted sequence id

for the child widgets. The sequence of identification numbers shows the line of the widget.

+3


source







All Articles