[Python-es] Coordenadas del mouse en pygtk

Arnau Sanchez pyarnau en gmail.com
Mie Ene 20 00:46:19 CET 2010


On 19/01/10 21:39, Ramiro wrote:

> Lo que quiero hacer es que por medio del mouse, haciendo click sobre la
> imagen y luego moviéndolo pueda ir moviendo la parte visible de la imagen.
>
> Mis preguntas son:
> ¿cómo puedo saber la posición del mouse?
> ¿cómo saber si hay algún botón presionado?

El procedimiento estándar es envolver el objeto que no recibe eventos -como en 
este caso gtk.Image- con un gtk.EventBox. Ver:

http://www.pygtk.org/pygtk2tutorial/ch-ContainerWidgets.html#sec-EventBox
http://www.pygtk.org/docs/pygtk/class-gtkeventbox.html

El script podría quedar así:

import gtk

def motion_notify(widget, event):
     print "x=%d y=%d" % (event.x, event.y)
def button_notify(widget, event):
     print "button=%d x=%d y=%d" % (event.button, event.x, event.y)

window = gtk.Window()
box = gtk.EventBox()
image = gtk.Image()
image.set_from_file("a.png")
box.add(image)
window.add(box)

box.add_events(gtk.gdk.POINTER_MOTION_MASK)
box.connect("motion_notify_event", motion_notify)
box.connect("button-press-event", button_notify)
window.show_all()

gtk.main()

-- 
Desarrollador freelance
http://www.arnau-sanchez.com



Más información sobre la lista de distribución Python-es