[pygtk] pygtk embeded pygame and DnD?
Hart's Antler
bhartsho at yahoo.com
Sat Feb 24 11:50:52 WST 2007
hi all, by the way, i just started learning pygtk and think its great.
i am having some problems getting drag and drop to work, and i'm not sure if its something simple
that i am doing wrong in my lack of understanding pygtk, or if there is a slight incompatibly
between pygtk and pygame. I have two toplevel windows, one is my GUI, and the other is just a
container for pygame. I have images in one window that i am making drag sources, these i can drag
to the other window with no problems. Here is the code snippet for the drag sources...
gimg = gtk.Image()
...
f = gtk.EventBox()
f.add( gimg )
f.drag_source_set(gtk.gdk.BUTTON1_MASK,
[ ('hello', gtk.TARGET_SAME_APP,0) ],
gtk.gdk.ACTION_COPY)
the problem i have is when i drop it on my other window, this window is an opengl pygame window.
I am expecting that on the drop my function drop_cb will be called, but it does not. Code
snippet..
win = gtk.Window( gtk.WINDOW_TOPLEVEL )
custom = gtk.DrawingArea()
custom.set_size_request( screensize[0], screensize[1] )
custom.connect('map-event', init_pygame)
win.drag_dest_set(0, [ ('hello', gtk.TARGET_SAME_APP,0) ], 0)
win.connect('drag_drop', drop_cb)
win.add( custom )
win.show_all()
shouldn't win.connect('drag_drop', drop_cb) catch all drop events and call the function? Or have
i set something wrong in win.drag_dest_set(); i'm a little confused about the params that it
takes, even after reading http://www.pygtk.org/pygtk2tutorial/sec-DNDMethods.html
below is the entire script,
-brett
import sys, os
# http://www.daa.com.au/pipermail/pygtk/2006-September/012888.html
if sys.platform == "win32":
os.environ['SDL_VIDEODRIVER'] = 'windib'
import pygtk, gtk
import twisted, twisted.web, twisted.internet
import Image, StringIO
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
print 'imported OpenGL'
from twisted.internet import gtk2reactor
gtk2reactor.install()
from twisted.internet import reactor
Kernel = reactor
pygame = None
screensize = ( 900, 680 )
def init_pygame( widget, *args ):
global pygame
import pygame
print 'imported pygame'
handle = widget.window.xid
os.environ['SDL_WINDOWID'] = str(handle)
pygame.init()
pygame.display.init()
pygame.mixer.init( 22050, -16, 1 )
info = pygame.display.Info()
print info
pygame.display.set_mode(screensize, pygame.OPENGL | pygame.DOUBLEBUF )
glLoadIdentity()
glViewport(0, 0, screensize[0], screensize[1])
glShadeModel(GL_SMOOTH)
glClearColor(0, 0, 0, 0)
glClearDepth(1.0)
glDisable(GL_LIGHTING)
glEnable(GL_POINT_SMOOTH)
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE)
glEnable(GL_COLOR_MATERIAL)
print '---- opengl defaults set'
# setup projection
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho( 0, 1, 0, 1, -1000, 1000 ) # left, right, bottom, top, zNear, zFar
# setup model view
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glDisable(GL_DEPTH_TEST)
glEnable(GL_SCISSOR_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glEnable( GL_NORMALIZE )
print '---- opengl view matrices set'
glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST)
glEnable(GL_LINE_SMOOTH) # enable antialiasing
glEnable(GL_POINT_SMOOTH)
width,height = screensize
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
if 0:
gluPerspective(160, 1.0*width/height, 0.01, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glShadeModel(GL_SMOOTH)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
# http://www.daa.com.au/pipermail/pygtk/2003-June/005268.html
def pil_to_gdk(img):
file = StringIO.StringIO()
img.save(file, 'ppm')
contents = file.getvalue()
file.close()
loader = gtk.gdk.PixbufLoader('pnm')
loader.write (contents, len(contents))
pixbuf = loader.get_pixbuf()
loader.close()
return pixbuf
def on_tab( w, data=None ): print w
window = gtk.Window( gtk.WINDOW_TOPLEVEL )
window.set_default_size( 300, 680 )
window.show()
notebook = gtk.Notebook()
window.add( notebook )
notebook.set_show_tabs(True)
child = gtk.ScrolledWindow(hadjustment=None)
child.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
vbox = gtk.VBox()
child.add_with_viewport( vbox )
vbox.show()
eventbox = gtk.EventBox()
eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
eventbox.connect('button-press-event', on_tab)
label = gtk.Label('add')
eventbox.add(label)
eventbox.show()
label.show()
notebook.append_page(child, eventbox)
child = gtk.Frame()
eventbox = gtk.EventBox()
eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
eventbox.connect('button-press-event', on_tab)
label = gtk.Label('edit')
eventbox.add(label)
eventbox.show()
label.show()
notebook.append_page(child, eventbox)
child = gtk.Frame()
eventbox = gtk.EventBox()
eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
eventbox.connect('button-press-event', on_tab)
label = gtk.Label('brain')
eventbox.add(label)
eventbox.show()
label.show()
notebook.append_page(child, eventbox)
window.show_all()
def drop_cb(wid, context, x, y, time):
print 'why is this never called?'
print context
print dir(context)
context.finish(True, False, time)
return True
win = gtk.Window( gtk.WINDOW_TOPLEVEL )
custom = gtk.DrawingArea()
custom.set_size_request( screensize[0], screensize[1] )
custom.connect('map-event', init_pygame)
win.drag_dest_set(0, [ ('hello', gtk.TARGET_SAME_APP,0) ], 0)
win.connect('drag_drop', drop_cb)
win.add( custom )
win.show_all()
dump = True
def mainloop():
#pygame.event.pump()
#k_pressed = pygame.key.get_pressed()
#m_pressed = pygame.mouse.get_pressed()
mx,my = custom.get_pointer()
sx,sy = screensize #global
mx = float(mx) / sx # zero division bug?
my = float(my) / sy
my = 1.0 - my # flip my
r,g,b,a = (0.5,0.5,0.5,0)
glClearColor(r,g,b,0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glDisable( GL_LIGHTING )
glDisable( GL_DEPTH_TEST )
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho( 0, 1, 0, 1, -1000, 1000 ) # left, right, bottom, top, zNear, zFar
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glDisable(GL_DEPTH_TEST)
glEnable(GL_SCISSOR_TEST)
glPushMatrix()
glLineWidth(8)
glColor3f(1,1,1)
glBegin( GL_LINE_LOOP )
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(0,1,0)
glEnd()
glPushMatrix()
glTranslate(mx, my, 0)
glScale(0.1,0.1,0.1)
glBegin(GL_TRIANGLES)
glColor3f(1.0,0.0,0.0)
glVertex3f( 0.0, 1.0, 0.0)
glColor3f(0.0,1.0,0.0)
glVertex3f(-1.0,-1.0, 1.0)
glColor3f(0.0,0.0,1.0)
glVertex3f( 1.0,-1.0, 1.0)
glColor3f(1.0,0.0,0.0)
glVertex3f( 0.0, 1.0, 0.0)
glColor3f(0.0,0.0,1.0)
glVertex3f( 1.0,-1.0, 1.0)
glColor3f(0.0,1.0,0.0)
glVertex3f( 1.0,-1.0, -1.0)
glColor3f(1.0,0.0,0.0)
glVertex3f( 0.0, 1.0, 0.0)
glColor3f(0.0,1.0,0.0)
glVertex3f( 1.0,-1.0, -1.0)
glColor3f(0.0,0.0,1.0)
glVertex3f(-1.0,-1.0, -1.0)
glColor3f(1.0,0.0,0.0)
glVertex3f( 0.0, 1.0, 0.0)
glColor3f(0.0,0.0,1.0)
glVertex3f(-1.0,-1.0,-1.0)
glColor3f(0.0,1.0,0.0)
glVertex3f(-1.0,-1.0, 1.0)
glEnd()
glPopMatrix()
glPopMatrix()
if pygame:
pygame.display.flip() # swap buffer
if dump: dump_screen()
Kernel.callLater(0.03, mainloop)
def dump_screen():
screen = pygame.display.get_surface()
x,y = screen.get_size()
data = pygame.image.tostring(screen, 'RGB', 0)
img = Image.fromstring('RGB', (x,y), data)
img.thumbnail( (128,128), True )
pixbuf = pil_to_gdk(img)
for i in range(10):
gimg = gtk.Image()
gimg.set_from_pixbuf( pixbuf )
gimg.show()
f = gtk.EventBox()
f.add( gimg )
f.drag_source_set(gtk.gdk.BUTTON1_MASK,
[ ('hello', gtk.TARGET_SAME_APP,0) ],
gtk.gdk.ACTION_COPY)
f.show()
vbox.add( f )
global dump
dump = False
#gtk.main()
Kernel.callLater(1, mainloop)
Kernel.run()
____________________________________________________________________________________
Looking for earth-friendly autos?
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
http://autos.yahoo.com/green_center/
More information about the pygtk
mailing list