Skip to content

Commit 69278d4

Browse files
committed
Claude 3.5 Sonnet's answer (the others were GPT-4o) to "compare example of mouse event handling for pysdl2, wxpython and pyglfw"
Comparison Feature PySDL2 wxPython pyGLFW Initialization Manual initialization wx.App setup glfw.init() setup Event Handling Polled in a loop Event binding (callbacks) Callback functions Mouse Button Events SDL_MOUSEBUTTONDOWN EVT_LEFT_DOWN glfw.set_mouse_button_callback Mouse Motion Events SDL_MOUSEMOTION EVT_MOTION glfw.set_cursor_pos_callback Ease of Use Intermediate Beginner-friendly Suitable for OpenGL apps Use Case Games and multimedia apps Desktop GUI applications OpenGL-based apps
1 parent 863e844 commit 69278d4

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

pyglfw_mouse_handling-v2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import glfw
2+
3+
def mouse_button_callback(window, button, action, mods):
4+
if action == glfw.PRESS:
5+
print(f"Mouse Button {button} Pressed")
6+
elif action == glfw.RELEASE:
7+
print(f"Mouse Button {button} Released")
8+
9+
def cursor_position_callback(window, xpos, ypos):
10+
print(f"Mouse Moved to {xpos}, {ypos}")
11+
12+
if __name__ == "__main__":
13+
if not glfw.init():
14+
raise Exception("GLFW cannot be initialized!")
15+
16+
window = glfw.create_window(800, 600, "pyGLFW Mouse Event Example", None, None)
17+
if not window:
18+
glfw.terminate()
19+
raise Exception("GLFW window cannot be created!")
20+
21+
glfw.set_mouse_button_callback(window, mouse_button_callback)
22+
glfw.set_cursor_pos_callback(window, cursor_position_callback)
23+
24+
glfw.make_context_current(window)
25+
while not glfw.window_should_close(window):
26+
glfw.poll_events()
27+
glfw.swap_buffers(window)
28+
29+
glfw.terminate()

pysdl2_mouse_handling-v2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sdl2
2+
import sdl2.ext
3+
4+
def run():
5+
sdl2.ext.init()
6+
window = sdl2.ext.Window("PySDL2 Mouse Event Example", size=(800, 600))
7+
window.show()
8+
9+
running = True
10+
while running:
11+
events = sdl2.ext.get_events()
12+
for event in events:
13+
if event.type == sdl2.SDL_QUIT:
14+
running = False
15+
elif event.type == sdl2.SDL_MOUSEBUTTONDOWN:
16+
print(f"Mouse Button Down at {event.button.x}, {event.button.y}")
17+
elif event.type == sdl2.SDL_MOUSEMOTION:
18+
print(f"Mouse Moved to {event.motion.x}, {event.motion.y}")
19+
window.refresh()
20+
21+
sdl2.ext.quit()
22+
23+
if __name__ == "__main__":
24+
run()

wxpython_mouse_handling-v2.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import wx
2+
3+
class MyFrame(wx.Frame):
4+
def __init__(self):
5+
super().__init__(None, title="wxPython Mouse Event Example", size=(800, 600))
6+
self.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
7+
self.Bind(wx.EVT_MOTION, self.on_mouse_motion)
8+
9+
def on_mouse_down(self, event):
10+
pos = event.GetPosition()
11+
print(f"Mouse Button Down at {pos.x}, {pos.y}")
12+
13+
def on_mouse_motion(self, event):
14+
if event.Dragging():
15+
pos = event.GetPosition()
16+
print(f"Mouse Dragging to {pos.x}, {pos.y}")
17+
18+
if __name__ == "__main__":
19+
app = wx.App(False)
20+
frame = MyFrame()
21+
frame.Show()
22+
app.MainLoop()

0 commit comments

Comments
 (0)