[Pythonmac-SIG] AppleEvent/Tkinter problem

Antoine Picard apicard at adobe.com
Wed Feb 25 17:51:53 EST 2004


I've been trying to get a Tkinter application to handle files that 
are dropped on the it's icon in the Dock.

I got a good bit of inspiration from __argvemulator_idle but it 
doesn't seem to get along with Tkinter too well.

If I don't add my own mainloop, it crashes right away. If I do add 
it, it will sometimes work the first time but never a second time. 
Clicking the button a few times before dropping the file on icon 
seems to increase the chance of it crashing. The crash is always a 
bus error.

The crash log follows the code below. Any idea what I'm doing wrong? 
Thanks for your help.

-----------------------------
#!/usr/bin/python
# -*- coding: <UTF-8> -*-

from Tkinter import *
from Carbon import AE
from Carbon.AppleEvents import *
from Carbon import Evt
from Carbon.Events import *
import aetools

class MyApp(Frame):
	def __init__(self, parent):
		Frame.__init__(self, master=parent, bd = 5, width=650)
		Pack.config(self)
		self.quitting=0

		self.createWidgets()
		#self.master.protocol("WM_DELETE_WINDOW", self.quit)

		self.ae_handlers = {}
		AE.AEInstallEventHandler('aevt', 'odoc', self.callback_wrapper)
		self.ae_handlers[('aevt', 'odoc')] = self.handlefiledrop

	def mainloop(self):
		TCL_ALL_EVENTS    = 0
		while not self.quitting:
			self.master.tk.dooneevent(TCL_ALL_EVENTS)
			self.dooneevent()

	def dooneevent(self, mask = highLevelEventMask, timeout = 1):
		got, event = Evt.WaitNextEvent(mask, timeout)
		if got:
			self.lowlevelhandler(event)

	def quit(self):
		self.quitting=1

	def lowlevelhandler(self, event):
		what, message, when, where, modifiers = event
		h, v = where
		if what == kHighLevelEvent:
			try:
				AE.AEProcessAppleEvent(event)
			except AE.Error, err:
				msg = "High Level Event: %s %s" % \
					(`hex(message)`, `hex(h | (v<<16))`)
				print 'AE error: ', err
				print 'in', msg
			return
		else:
			print "Unhandled event:", event


	def handlefiledrop(self, _object=None, **args):
		for alias in _object:
			fss = alias.FSResolveAlias(None)[0]
			pathname = fss.as_pathname()
			print pathname


	def callback_wrapper(self, _request, _reply):
		_parameters, _attributes = aetools.unpackevent(_request)
		_class = _attributes['evcl'].type
		_type = _attributes['evid'].type

		if self.ae_handlers.has_key((_class, _type)):
			_function = self.ae_handlers[(_class, _type)]
		elif self.ae_handlers.has_key((_class, '****')):
			_function = self.ae_handlers[(_class, '****')]
		elif self.ae_handlers.has_key(('****', '****')):
			_function = self.ae_handlers[('****', '****')]
		else:
			raise 'Cannot happen: AE callback without 
handler', (_class, _type)

		# XXXX Do key-to-name mapping here

		_parameters['_attributes'] = _attributes
		_parameters['_class'] = _class
		_parameters['_type'] = _type
		if _parameters.has_key('----'):
			_object = _parameters['----']
			del _parameters['----']
			# The try/except that used to be here can 
mask programmer errors.
			# Let the program crash, the programmer can 
always add a **args
			# to the formal parameter list.
			rv = apply(_function, (_object,), _parameters)
		else:
			#Same try/except comment as above
			rv = apply(_function, (), _parameters)

		if rv == None:
			aetools.packevent(_reply, {})
		else:
			aetools.packevent(_reply, {'----':rv})

	def createWidgets(self):
		top = Frame(self)
		top.pack(side=TOP)
		Button(top, text="Hello", command=self.hello).pack(side=LEFT)

	def hello(self):
		print "hello"

if __name__ == '__main__':
	main = Tk()
	app=MyApp(main)
	app.mainloop()

---------------------
Exception:  EXC_BAD_ACCESS (0x0001)
Codes:      KERN_PROTECTION_FAILURE (0x0002) at 0x00000008

Thread 0 Crashed:
  #0   0x10022184 in PyFrame_New (frameobject.c:540)
  #1   0x10024c34 in function_call (funcobject.c:504)
  #2   0x1000b884 in PyObject_Call (abstract.c:1756)
  #3   0x10014d14 in instancemethod_call (classobject.c:2433)
  #4   0x1000b884 in PyObject_Call (abstract.c:1756)
  #5   0x1007613c in PyEval_CallObjectWithKeywords (ceval.c:3347)
  #6   0x002fbfec in GenericEventHandler (_AEmodule.c:1421)
  #7   0x91b56570 in aeDispatchAppleEvent(AEDesc const*, AEDesc*, 
unsigned long, unsigned char*)
  #8   0x91b590cc in dispatchEventAndSendReply(AEDesc const*, AEDesc*)
  #9   0x91b56478 in aeProcessAppleEvent
  #10  0x96a83778 in AEProcessAppleEvent
  #11  0x0b07dfd8 in TkMacOSXDoHLEvent
  #12  0x0b07be18 in TkMacOSXProcessAppleEvent
  #13  0x0b07bf5c in TkMacOSXProcessEvent
  #14  0x0b07bff0 in ReceiveAndProcessEvent
  #15  0x0b07bdb4 in TkMacOSXCountAndProcessMacEvents
  #16  0x0b08762c in TkMacOSXWaitForEvent
  #17  0x0a079254 in Tcl_WaitForEvent
  #18  0x0a057774 in Tcl_DoOneEvent
  #19  0x0016e780 in Tkapp_DoOneEvent (_tkinter.c:2576)
  #20  0x100762a8 in call_function (ceval.c:3440)
  #21  0x10073e80 in eval_frame (ceval.c:2117)
  #22  0x100764f8 in fast_function (ceval.c:3520)
  #23  0x10076380 in call_function (ceval.c:3458)
  #24  0x10073e80 in eval_frame (ceval.c:2117)
  #25  0x10075198 in PyEval_EvalCodeEx (ceval.c:2663)
  #26  0x1007800c in PyEval_EvalCode (ceval.c:537)
  #27  0x100a8e6c in run_node (pythonrun.c:1206)
  #28  0x100a8618 in PyRun_SimpleFileExFlags (pythonrun.c:805)
  #29  0x100b4de8 in Py_Main (main.c:415)
  #30  0x00001b5c in 0x1b5c
  #31  0x000019dc in 0x19dc

Thread 1:
  #0   0x9002568c in select
  #1   0x0b0877fc in NotifierThreadProc
  #2   0x90020c28 in _pthread_body

PPC Thread State:
   srr0: 0x10022184 srr1: 0x0000f030                vrsave: 0x00000000
    xer: 0x00000000   lr: 0x10022180  ctr: 0x1002215c   mq: 0x00000000
     r0: 0x10074934   r1: 0xbfffe4b0   r2: 0x10106a68   r3: 0x00000000
     r4: 0x00119920   r5: 0x000a29c0   r6: 0x00000000   r7: 0x4800224c
     r8: 0x10074934   r9: 0x00000000  r10: 0x00000000  r11: 0x1010fb68
    r12: 0x1002215c  r13: 0x00000000  r14: 0x00000000  r15: 0x101019ac
    r16: 0x00000000  r17: 0x00000000  r18: 0x10071d08  r19: 0x0000020c
    r20: 0x00000000  r21: 0x00000000  r22: 0x000a29c0  r23: 0x00000000
    r24: 0x00000000  r25: 0x00119920  r26: 0x00000000  r27: 0x00119920
    r28: 0x00000000  r29: 0x0034cbcc  r30: 0x00000000  r31: 0x100748b4


-- 

				Antoine Picard
				Type Department
				Adobe Systems, Inc.



More information about the Pythonmac-SIG mailing list