I have been working with creating some code that I can use in future in order to embed a pygame window within a tkinter window in order to make use of tkinter menus and buttons. I am currently having some issues with dealing with key presses. i want all key presses to be dealt with by pygame rather than tkinter so that if the pygame element is made fullscreen (thus meaning tkinter is not used) then tkinter key bindings are left ignored.
My problem is that when the window is initially opened (or after it has been clicked off and back on again), only tkinter is registering key bindings. Once the user clicks on the pygame window, only pygame registers key bindings. My question is how can I detect whether tkinter or pygame is detecting the key presses and also how can I make it so that pygame detects the presses rather than tkinter when I have detected it?
""" Object for creating a pygame window embedded within a tkinter window.
pygame_minsize - tuple - The minimum size of the pygame window.
pygame_maxsize - tuple - The maximum size of the pygame window.
If none is specified no restrictions are placed.
Note: This includes the pygame screen even when fullscreen.
used to contain widgets to interact with the pygame window.
If none is specified the frame is not included.
menu - boolean - Whether a menu bar should be included.
the tkinter window. This should be called every frame.
""" Closes the open window."""
dependent on the value of the fullscreen attribute.
If there is no direct solution (of which I do not know), you could make a handler that passes the keypresses detected in tkinter to pygame.
The idea is to bind the keys to dispatch to a dispatch_event_to_pygame function, which will create a corresponding pygame.event.Event object, and to inject the latter into pygame's event loop, through the pygame.event.post function.
Not the answer you're looking for? Browse other questions tagged python python-3.x tkinter pygame keypress or ask your own question.