You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calling cget on a widget typically returns a str value, but when called on a custom widget class that inherits from ttk, cget returns _tkinter.Tcl_Obj instead. This issue was demonstrated in this StackOverflow Question, where it was suggested that I submit this bug report.
EXAMPLE:
importtkinterastkfromtkinterimportttk# some tkinter boilerplate (i.e. 'root = tk.Tk()') has been left out for brevity# tk Entry widgettk_entry=tk.Entry()
print(type(tk_entry.cget('fg')))
# => <class 'str'># ttk Entry widgetttk_entry=ttk.Entry()
print(type(ttk_entry.cget('foreground')))
# => <class 'str'># subclassed ttk Entry widgetph_entry=PlaceholderEntry()
print(type(ph_entry.cget('foreground')))
# => <class '_tkinter.Tcl_Obj'> ... Why is this different?# ultra basic subclass widgetbasic_entry=BasicEntry()
print(type(basic_entry.cget('foreground')))
# => <class 'str'># demo class inheriting from ttk.Entry (as shown on StackOverflow)classPlaceholderEntry(ttk.Entry):
"""Entry widget with focus-toggled placeholder text"""def__init__(
self, parent, placeholder='', color='#828790', *args, **kwargs
):
super().__init__(parent, *args, **kwargs)
self.placeholder=placeholderself._ph_color=colorself._default_fg=self.cget('foreground') # default foreground color# focus bindingsself.bind('<FocusOut>', self.set_placeholder)
self.bind('<FocusIn>', self.clear_placeholder)
# initialize placeholderself.set_placeholder()
defset_placeholder(self, *args): # on focus outifnotself.get(): # if the entry has no text...self.insert(0, self.placeholder)
self.configure(foreground=self._ph_color)
defclear_placeholder(self, *args): # on focus inifstr(self.cget('foreground')) ==self._ph_color: # str() is used here to make this work as intendedself.delete(0, tk.END)
self.configure(foreground=self._default_fg)
# minimal demo class inheriting from ttk.Entry that *doesn't* have this issueclassBasicEntry(ttk.Entry):
def__init__(self, parent):
super().__init__(parent)
Your environment
Running code directly from VSCode
CPython versions tested on: 3.11.0, 64-bit
Operating system and architecture: Windows 10
The text was updated successfully, but these errors were encountered:
I've encountered an issue that might be related. I was having trouble debugging a function that called cget and sometimes returned a _tkinter.Tcl_Obj, and discovered that the reason it felt impossible to debug was due to print statements affecting the outcome.
Tkinter converts some Tcl types (numbers, strings, etc) to corresponding Python types and use Tcl_Obj to wrap all other types. You can use the string attribute or call str() to get the string representation for them.
The second issue is a duplicate of #101830. It was just fixed, wait for the next bugfix release.
Bug report
Calling
cget
on a widget typically returns astr
value, but when called on a custom widget class that inherits fromttk
,cget
returns_tkinter.Tcl_Obj
instead. This issue was demonstrated in this StackOverflow Question, where it was suggested that I submit this bug report.EXAMPLE:
Your environment
The text was updated successfully, but these errors were encountered: