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
Comment thread rendercanvas/tk.py
Comment thread rendercanvas/tk.py Outdated
@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...

Comment thread rendercanvas/tk.py
Comment on lines +324 to +325
# Tk/macOS already works in device pixels for this purpose.
return 1.0

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.

Unfortunately, this is not the case. (And if it was the case, we'd have to scale width/height in several other places.)

But since on MacOS it already operates in bitmap mode, we basically have the fallback I proposed, but only for MacOS.

Have you been able to test this on a hidpi screen on Windows and/or Linux?

@Lcbx Lcbx Aug 27, 2026

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.

tested it on windows 11 on the tk_app example
(I have only 1 monitor so I have to logout login between display scale changes)

scaling = 100
resize event : 876 650
winfo: 1000 650
tk scaling: 1.3339898243886428
1 inch: 96.04726735598227

scaling = 150
resize event : 876 650
winfo: 1000 650
tk scaling: 1.332282448377581
1 inch: 95.92433628318584

code used

print("resize event:", event.width, event.height)
print("winfo:",        loop._root.winfo_width(), loop._root.winfo_height())
print("tk scaling:",   loop._root.tk.call("tk", "scaling"))
print("1 inch:",       loop._root.winfo_fpixels("1i"))

it seems windows automatically scales dpi-unaware processes

So behavior is probably fine on windows

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