GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Tkinter ttk widgets cget() returning _tkinter.Tcl_Obj instead of str · Issue #126008 · python/cpython

Tkinter ttk widgets cget() returning _tkinter.Tcl_Obj instead of str #126008

New issue
Copy link
New issue
Copy link
Closed
Closed
Tkinter ttk widgets cget() returning _tkinter.Tcl_Obj instead of str#126008
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirtopic-tkinter

Description

@maj113
maj113
opened on Oct 26, 2024
Issue body actions

The docstring: """Return the resource value for a KEY given as string.""" claims it returns as a string however it returns a _tkinter.Tcl_Obj
which seems to lack __eq__/the ability to compare it to a string so something like this:

is_normal = radio_button.cget("state") == tk.NORMAL

Will always return false no matter what unless its wrapped in a str()

I propose either clarifying the docstr or returning as a str() wrapped obj but i presume that's a breaking change

Linked PRs

  • gh-126008: Fix the type of return value for ttk.Widget.cget #126102
  • gh-126008: Improve docstrings for Tkinter cget and configure methods #133303
  • [3.14] gh-126008: Improve docstrings for Tkinter cget and configure methods (GH-133303) #137660
  • [3.13] gh-126008: Improve docstrings for Tkinter cget and configure methods (GH-133303) #137661
Reacted by Jesse205

Activity

  1. maj113
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Oct 26, 2024
  2. picnixz
    added
    topic-tkinter
    on Oct 26, 2024
  3. Xiaokang2022 commented on Oct 27, 2024

    @Xiaokang2022
    Xiaokang2022
    on Oct 27, 2024
    Contributor
    More actions

    Hello, @maj113

    The result of my test is no problem, my environment is as follows:

    • OS: Windows11 23H2
    • Python: 3.13.0

    The code I tested is as follows:

    import tkinter
    
    root = tkinter.Tk()
    
    radio_button = tkinter.Radiobutton(root)
    state = radio_button.cget("state")
    is_normal = state == tkinter.NORMAL
    
    print(f"{is_normal=}, {type(state)=}")

    The output is as follows:

    is_normal=True, type(state)=<class 'str'>

    Obviously, it's not a _tkinter.Tcl_Obj. Therefore you should specifically point out the environment, as this may be a problem that only arises in certain environments. If there's a problem with the way I'm testing, feel free to point it out.

  4. maj113 commented on Oct 27, 2024

    @maj113
    maj113
    on Oct 27, 2024
    Author
    More actions

    Obviously, it's not a _tkinter.Tcl_Obj. Therefore you should specifically point out the environment, as this may be a problem that only arises in certain environments. If there's a problem with the way I'm testing, feel free to point it out.

    I'm testing this on Windows 11 24H2 with the same behavior on (C)python 3.12.5 and 3.13.0

  5. maj113 commented on Oct 27, 2024

    @maj113
    maj113
    on Oct 27, 2024
    Author
    More actions

    okay the issue occurs ONLY with ttk widgets, not regular tk ones
    image

  6. Xiaokang2022 commented on Oct 29, 2024

    @Xiaokang2022
    Xiaokang2022
    on Oct 29, 2024
    Contributor
    More actions

    Ok, I'll try to create a PR later to fix this.

    I don't think this should be solved just by modifying the docstring, _tkinter.Tcl_Obj should be converted to a string type.

    Reacted by Maj Soklič
  7. maj113
    changed the title [-]Tkinter widget cget() misleading documentation about returning as a string[/-] [+]Tkinter ttk widgets cget() returning _tkinter.Tcl_Obj instead of str[/+] on Oct 29, 2024
  8. brianzhouzc commented on Oct 29, 2024

    @brianzhouzc
    brianzhouzc
    on Oct 29, 2024
    More actions

    I've encountered the same issue yesterday by accessing the state using the key directly. Took me way to long to figure out that the state isn't string type.

    self.entry_box = ttk.Entry(self.root, width=40)
    
    # This doesn't work!
    if (self.entry_box['state'] == 'normal'):
        print('State is normal')

    The weird thing is, if you access the state before hand by printing it as a string, then it becomes a string type.

    import tkinter as tk
    from tkinter import ttk, messagebox
    
    class App:
        def __init__(self, root):
            self.root = root
    
            self.entry_box = ttk.Entry(self.root, width=40)
            self.entry_box.pack(padx=10, pady=10)
    
            messagebox.showinfo(message=f"Type: {type(self.entry_box['state'])}\nState: {self.entry_box['state']}\nType after access: {type(self.entry_box['state'])}")
    
    if __name__ == "__main__":
        root = tk.Tk()
        app = App(root)
        root.mainloop()

    Result:

    Type: <class '_tkinter.Tcl_Obj'>
    State: normal
    Type after access: <class 'str'>
  9. Xiaokang2022 commented on Oct 29, 2024

    @Xiaokang2022
    Xiaokang2022
    on Oct 29, 2024
    Contributor
    More actions

    Hello, @brianzhouzc

    I tested your code and it turned out to be different from yours:

    Result:

    Type: <class '_tkinter.Tcl_Obj'>
    State: normal
    Type after access: <class '_tkinter.Tcl_Obj'>

    Perhaps you should point out your environment.

  10. terryjreedy commented on Oct 29, 2024

    @terryjreedy
    terryjreedy
    on Oct 29, 2024
    Member
    More actions

    @serhiy-storchaka We need a decision on what to do before too much effort is put into PRs.

  11. serhiy-storchaka commented on Oct 29, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 29, 2024
    Member
    More actions

    The docstring is outdated. Perhaps it was written when all low-level Tkinter API returned only strings. "as string" should be removed.

  12. brianzhouzc commented on Oct 29, 2024

    @brianzhouzc
    brianzhouzc
    on Oct 29, 2024
    More actions

    Hello, @brianzhouzc

    I tested your code and it turned out to be different from yours:

    Result:

    Type: <class '_tkinter.Tcl_Obj'>
    State: normal
    Type after access: <class '_tkinter.Tcl_Obj'>

    Perhaps you should point out your environment.

    Apologies, my environment is Windows 10 Business 22H2 19045.5011, Python 3.11.9.

    Tried on Python 3.12.6 as well and the behaviours is indeed different, like what you have in your reply.

  13. maj113 commented on Oct 29, 2024

    @maj113
    maj113
    on Oct 29, 2024
    Author
    More actions

    The docstring is outdated. Perhaps it was written when all low-level Tkinter API returned only strings. "as string" should be removed.

    I don't think just removing that would be enough to clarify the return value, it still might confuse people that the same key behaves differently comparing tk vs ttk, I haven't checked how tkinter.Tcl_Obj is implemented but maybe it can be extended with a eq so that at least string comparisons work as expected without the need to wrap it in a str

    Reacted by Jesse205
  14. serhiy-storchaka commented on Oct 29, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 29, 2024
    Member
    More actions

    This is a Tk implementation detail. Depending on the key, the Tk version, the way of creating and modifying the widget, you can get a number, a boolean, a string, a tuple, a Tcl_Obj. If you get a Tcl_Obj and need a string, just call str().

    Making Tcl_Obj comparable with str is an interesting idea, but this is a new feature which can only be added in a new Python version. It may require significant internal changes in Tkinter.

    Reacted by Maj Soklič and Zhikang Yan
  15. Xiaokang2022 commented on Oct 30, 2024

    @Xiaokang2022
    Xiaokang2022
    on Oct 30, 2024
    Contributor
    More actions

    The docstring is outdated. Perhaps it was written when all low-level Tkinter API returned only strings. "as string" should be removed.

    Is it possible that the "as string" in the comment refers to the KEY, not the return value? This can really be misleading.

    But beyond that, it's also a problem that widgets of tk don't behave consistently with widgets of ttk.

  16. picnixz commented on Oct 30, 2024

    @picnixz
    picnixz
    on Oct 30, 2024
    Member
    More actions

    What I understood wrongly: the key is a string, the return value is a string as well because everything is stored as a string. But this was wrong! (I think?)

    The key is a string, the return value is anything. Am I correct now?

  17. Xiaokang2022 commented on Oct 30, 2024

    @Xiaokang2022
    Xiaokang2022
    on Oct 30, 2024
    Contributor
    More actions

    The key is a string, the return value is anything. Am I correct now?

    I think so.

  18. willingc
    added this to docs issueson Oct 31, 2024
  19. github-project-automation
    moved this to Todo in docs issueson Oct 31, 2024
  20. Xiaokang2022 commented on Jan 7, 2025

    @Xiaokang2022
    Xiaokang2022
    on Jan 7, 2025
    Contributor
    More actions

    I added tests to the PR, can anyone help review that?

    Is it possible that the "as string" in the comment refers to the KEY, not the return value? This can really be misleading.
    But beyond that, it's also a problem that widgets of tk don't behave consistently with widgets of ttk.

    Also, as mentioned above, this should be more than just a docs issue.

  21. serhiy-storchaka
    added a commit that references this issue on May 2, 2025

    pythongh-126008: Improve docstrings for Tkinter cget and configure me…

    c7f9234
  22. serhiy-storchaka commented on May 2, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on May 2, 2025
    Member
    More actions

    To me, it looks like saying that KEY is a string. This is redundant and may be confusing, so it is is better to remove it. There are also other issues with docstrings of cget() and configure()` methods. #133303 tryes to improve them.

    Reacted by Zhikang Yan and Maj Soklič
  23. serhiy-storchaka
    added a commit that references this issue on Aug 12, 2025

    gh-126008: Improve docstrings for Tkinter cget and configure methods (G…

    027cacb
  24. miss-islington
    added 2 commits that reference this issue on Aug 12, 2025

    pythongh-126008: Improve docstrings for Tkinter cget and configure me…

    12f491a

    pythongh-126008: Improve docstrings for Tkinter cget and configure me…

    b2568e8
  25. serhiy-storchaka
    closed this as completedon Aug 12, 2025
  26. serhiy-storchaka
    added a commit that references this issue on Aug 12, 2025

    [3.13] gh-126008: Improve docstrings for Tkinter cget and configure m…

    4a630d7
  27. maurycy
    added a commit that references this issue on Aug 12, 2025

    Merge branch 'main' into csv-sniffer-counter-set

    11c891d
  28. Agent-Hellboy
    added a commit that references this issue on Aug 19, 2025

    pythongh-126008: Improve docstrings for Tkinter cget and configure me…

    d7253bd
  29. encukou
    added a commit that references this issue on Oct 7, 2025

    [3.14] gh-126008: Improve docstrings for Tkinter cget and configure m…

    181cecc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc dirtopic-tkinter

    Projects

    • docs issues
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.