Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wx: delegate event handling to subwidget #665

Closed
wants to merge 1 commit into from

Conversation

gselzer
Copy link

@gselzer gselzer commented Jan 21, 2025

Found while working on pyapp-kit/ndv#114 that WxWgpuCanvas wasn't responding to events the way that I expected. With this change, we can react to events.

I didn't see any location within the tests folder that looked like a good location for automated testing code, but I manually tested this change using a modification of gui_wx.py, which prints many lines with the change and does not without the change. Tested on Windows 11, Python 3.11 and latest main:

"""
Run the triangle/cube example in the wx GUI backend.
"""

# run_example = false

import wx
from wgpu.gui.wx import WgpuCanvas

from triangle import setup_drawing_sync
# from cube import setup_drawing_sync


app = wx.App()
canvas = WgpuCanvas(title=f"Triangle example on {WgpuCanvas.__name__}")

draw_func = setup_drawing_sync(canvas)
canvas.request_draw(draw_func)

def on_mouse_move(event: wx.MouseEvent):
    print("Captured mouse event!")

canvas.Bind(wx.EVT_MOTION, on_mouse_move)

app.MainLoop()

Happy to add an automated test if there's somewhere that makes sense. Also, I'm also pretty naive about wx, so unsure about the correctness of the behavior (e.g. should calls to super().Bind()/super().Unbind() occur before calling them on the subwidget?

@gselzer gselzer requested a review from Korijn as a code owner January 21, 2025 22:59
@almarklein
Copy link
Member

Hi!

There's two ways that we intend that users use these canvas classes:

Use it as an abstract canvas, using the event-system (using canvas.add_event_handler()) that works the same for all the backends. You don't really care whether its wx or qt or glfw.

Or, using wx explicitly, probably because you're embedding it in a wx GUI. In this case you should probably use the WxWgpuWindow (or WxRenderWidget in rendercanvas).

If you want to use a toplevel wx canvas using wx events, I'd recommend either:

  • import WxWgpuWindow and embed it in your own frame.
  • import WxWgpuCanvas and maybe subclass it to expose the subwidget as a public property to which other code can Bind().

@almarklein
Copy link
Member

Oh, and consider using rendercanvas which will replace wgpu.gui. Its (nearly) fully compatible, and improved in several ways.

@gselzer
Copy link
Author

gselzer commented Feb 5, 2025

f you want to use a toplevel wx canvas using wx events, I'd recommend either:

* import `WxWgpuWindow` and embed it in your own frame.

* import `WxWgpuCanvas` and maybe subclass it to expose the subwidget as a public property to which other code can `Bind()`.

This is a good point, gotcha.

Do you still see value in this PR, or should we close it?

@almarklein
Copy link
Member

Closing :)

@almarklein almarklein closed this Feb 7, 2025
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