Skip to content

Optimize the speed of multiple calls of method winfo_rgb of tkinter widgets by caching #128576

@Xiaokang2022

Description

@Xiaokang2022

Feature or enhancement

Proposal:

Just run the code below:

import timeit

print(timeit.timeit("tk.winfo_rgb('red')", "import tkinter; tk = tkinter.Tk()"))
# 11.070131499999661

# Control group:
print(timeit.timeit("tk.winfo_id()", "import tkinter; tk = tkinter.Tk()"))
# 0.4995561000005182

You can see that this operation is too slow. The method winfo_rgb is also specific to the return value for a specific input, so I tried using decorator functools.lru_cache to speed up multiple calls to the method.

Here's what happens when using decorators:

import timeit

print(timeit.timeit("tk.winfo_rgb('red')", "import tkinter; tk = tkinter.Tk()"))
# 0.036084200000004785

If a user doesn't test the speed of the method, he/she may never know that the method is called so slowly (This can cause him/her to call the method with the same parameter multiple times), so I think it's necessary to speed up multiple calls to the method by caching.

My test environment:

  • Python: 3.13.0
  • OS: Windows 24H2

If there are any problems with my idea, please feel free to point it out, thank you!

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytopic-tkintertype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions