ARTICLE AD BOX
Here is an example program, which sets two options text and padding for a label.
import tkinter as tk import tkinter.ttk root = tk.Tk() root.geometry("300x200") label = tk.ttk.Label(root, background="red") label.pack() label.configure(text="Label") print(label.configure("text")[-1]) label.configure(padding=(100, 50)) print(label.configure("padding")[-1]) root.mainloop()
Looking at the print output, the text option has been returned as a string "Label", as expected. However, padding, despite being set to a tuple of integer values, is now being returned as a tuple of "pixel objects":
Label (<pixel object: '100'>, <pixel object: '50'>)What objects are those? Am I using the wrong method to read the values? Is there a built-in method to parse these objects that I should be using?
Researching online, I found a few different methods to read a widget's options, but they all yield the same result:
>>> label.configure("padding")[-1] (<pixel object: '100'>, <pixel object: '50'>) >>> label.cget("padding") (<pixel object: '100'>, <pixel object: '50'>) >>> label["padding"] (<pixel object: '100'>, <pixel object: '50'>)Further investigation of the "pixel objects":
>>> type(label.configure("padding")[-1][0]) <class '_tkinter.Tcl_Obj'> >>> dir(label.configure("padding")[-1][0]) ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'string', 'typename']My system for the results from above runs:
>>> sys.version '3.9.2 (default, Feb 28 2021, 17:03:44) \n[GCC 10.2.1 20210110]' >>> tk.Tcl().call("info", "patchlevel") '8.6.11'Switching to a different system:
>>>> sys.version '3.11.13 (413c9b7f57f5, Jul 03 2025, 18:03:56)\n[PyPy 7.3.20 with GCC 10.2.1 20210130 (Red Hat 10.2.1-11)]' >>>> tk.Tcl().call("info", "patchlevel") '8.6.14'Now, I get a different return value, which is simply a tuple of integers:
>>>> label.configure("padding")[-1] (100, 50)Since the version apparently influences the return values, wouldn't it make even more sense to have an abstract built-in method available to parse these values?
