Skip to content

support for tkinter backend. tested on windows 11 - #244

Open
Lcbx wants to merge 9 commits into
pygfx:mainfrom
Lcbx:tk_backend
Open

support for tkinter backend. tested on windows 11#244
Lcbx wants to merge 9 commits into
pygfx:mainfrom
Lcbx:tk_backend

Conversation

@Lcbx

@Lcbx Lcbx commented Jul 28, 2026

Copy link
Copy Markdown

added support for tkinter backend.
imageimage

tested the PR a bunch on my local machine (windows 11)
feedback from X11 / XWayland is welcome

Disclaimer : I used AI, though I nitpicked it to death for maintainability / respect to the repo's coding style
I would understand if you told me to eat sh** nonetheless
CONTRIBUTING.md seems to allow responsible vibe coding. Yay !

@Lcbx
Lcbx force-pushed the tk_backend branch 3 times, most recently from 6d3c0a3 to 76b81aa Compare July 28, 2026 22:47
@almarklein

Copy link
Copy Markdown
Member

Thanks for this! I'm working on an open pr (#227) that changes requires changes to the backends. I'll come back to this pr once that is done.

Comment thread rendercanvas/core/loop.py
@Lcbx

Lcbx commented Aug 6, 2026

Copy link
Copy Markdown
Author

FYI this PR is ready for review (and compatible with #227)

@almarklein almarklein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I made some comments.

On MacOS this backend is really slow for large resolutions, but there is some room for improvement for the bitmap mode, so this might be improved.

Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
Comment thread rendercanvas/tk.py Outdated
@Lcbx

Lcbx commented Aug 19, 2026

Copy link
Copy Markdown
Author

I think this addresses everything
(I answered comments after making the changes asked, that's why it may seem fast)

@almarklein almarklein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also some linting errors. Easy to fix: run ruff format and ruff lint to check locally.

The pixel ratio it a bigger problem. If the reported physical size is incorrect, rendering to screen on a hidpi display will error, because the textures don't match size. I spent some time (with help with both tk docs and Claude) trying to get the pixel ratio from tk, but without luck. It may not be possible.

For bitmap mode it's less of a problem, because tk will simply scale the bitmap. Therefore, a quick solution would be to only support bitmap mode, at least for now.

If we do want to support pixel ratio, we'd have to query the OS directly, which involves a different solution for each OS, which gets ugly especially on x11 vs wayland, so I'd rather not go there for this particular backend.

Comment thread rendercanvas/tk.py Outdated
raise ValueError(f"Unsupported bitmap format {format!r}")

try:
rgba = np.asarray(memoryview(data))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big thing, but just slightly shorter.

Suggested change
rgba = np.asarray(memoryview(data))
rgba = np.asarray(data)

Comment thread rendercanvas/tk.py
if isinstance(self.master, _RenderToplevel):
self.master.geometry(f"{width}x{height}")
else:
self.configure(width=width, height=height)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what configuring the width and height does? I could not find it. Would be good to add that as a comment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geometry() asks the window manager to resize the enclosing Toplevel/window to that size.
configure() set the widget requested size

Comment thread rendercanvas/tk.py Outdated
Comment on lines +449 to +450
def set_title(self, title: str) -> None:
self.root.title(title)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern to follow is to implement _rc_set_title() on the widget, which then does something like:

        if isinstance(self.master, _RenderToplevel):
            self.master.root.title(title)

@Lcbx

Lcbx commented Aug 22, 2026

Copy link
Copy Markdown
Author

The pixel ratio it a bigger problem. If the reported physical size is incorrect, rendering to screen on a hidpi display will error, because the textures don't match size. I spent some time (with help with both tk docs and Claude) trying to get the pixel ratio from tk, but without luck. It may not be possible.

I lifted the logical size computation from matplotlib tk backend (license is permissive PSF/BSD-compatible).
If it's good enough for them...

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants