0

I'm embedding a pygame display in a GTK widget to implement debug controls for a game prototype.

It seems I'm forced to use PyGTK to handle the inputs for the game. My callbacks receive the gtk key-press-events, but button-press-events never makes it.

class Dashboard():
[...]
self.drawingarea_game = gtk.DrawingArea()
self.drawingarea_game.set_flags(gtk.CAN_DEFAULT | gtk.CAN_FOCUS | gtk.SENSITIVE | gtk.PARENT_SENSITIVE)
gobject.timeout_add(60, game.test_loop)
self.win.show_all()
self.drawingarea_game.grab_focus()
self.drawingarea_game.connect("button-press-event", game.mousepress)
self.drawingarea_game.connect("key-press-event", game.keypress)
gtk.main()


class Game():
[...]
def keypress(self, widget, event):
    Error.log(event.keyval)
    if event.keyval == 100: #d
        self.SCREEN_POSITION_X = -30
    if event.keyval == 97:  #a
        self.SCREEN_POSITION_X = 0
    if event.keyval == 115: #s
        self.SCREEN_POSITION_Y = -30
    if event.keyval == 119: #w
        self.SCREEN_POSITION_Y = 0
    return True

def mousepress(self, widget, event):
    Error.log(event.button)

I also tried checking for a pygame event, in case it somehow got hijacked, but that doesn't report anything either:

class Game():
[...]
def test_loop(self):
for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
        print(event)

The drawing area is nested so: Window -> HBox -> VBox

I have a couple of buttons elsewhere in the window that receive clicks fine.

Any ideas?

1 Answer 1

1

The mouse event has to be explicitly declared for the drawing area, apparently in contrast to the keystroke event. It works after adding:

self.drawingarea_game.set_events(gtk.gdk.BUTTON_PRESS_MASK)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.